Best Jmock-library code snippet using org.jmock.internal.ReturnDefaultValueAction
Source:ReturnDefaultValueActionTests.java
...5import junit.framework.TestCase;6import org.hamcrest.StringDescription;7import org.jmock.api.Imposteriser;8import org.jmock.api.Invocation;9import org.jmock.internal.ReturnDefaultValueAction;10import org.jmock.lib.JavaReflectionImposteriser;11import org.jmock.test.unit.support.AssertThat;12import org.jmock.test.unit.support.MethodFactory;13public class ReturnDefaultValueActionTests extends TestCase {14 static final Object[] NO_ARG_VALUES = new Object[0];15 static final MethodFactory METHOD_FACTORY = new MethodFactory();16 private ReturnDefaultValueAction action;17 @Override public void setUp() {18 action = new ReturnDefaultValueAction();19 }20 public void testWritesDescritionToStringBuffer() {21 AssertThat.stringIncludes("contains expected description",22 "returns a default value",23 StringDescription.toString(action));24 }25 public void testReturnsUsefulDefaultResultsForBasicTypes()26 throws Throwable27 {28 assertHasRegisteredValue(action, boolean.class, Boolean.FALSE);29 assertHasRegisteredValue(action, void.class, null);30 assertHasRegisteredValue(action, byte.class, new Byte((byte)0));31 assertHasRegisteredValue(action, short.class, new Short((short)0));32 assertHasRegisteredValue(action, int.class, new Integer(0));33 assertHasRegisteredValue(action, long.class, new Long(0L));34 assertHasRegisteredValue(action, char.class, new Character('\0'));35 assertHasRegisteredValue(action, float.class, new Float(0.0F));36 assertHasRegisteredValue(action, double.class, new Double(0.0));37 assertHasRegisteredValue(action, Boolean.class, Boolean.FALSE);38 assertHasRegisteredValue(action, Byte.class, new Byte((byte)0));39 assertHasRegisteredValue(action, Short.class, new Short((short)0));40 assertHasRegisteredValue(action, Integer.class, new Integer(0));41 assertHasRegisteredValue(action, Long.class, new Long(0L));42 assertHasRegisteredValue(action, Character.class, new Character('\0'));43 assertHasRegisteredValue(action, Float.class, new Float(0.0F));44 assertHasRegisteredValue(action, Double.class, new Double(0.0));45 assertHasRegisteredValue(action, String.class, "");46 assertNotNull( "should return an object for Object return type",47 action.invoke(invocationReturning(Object.class)) );48 }49 public void testReturnsEmptyArrayForAllArrayTypes()50 throws Throwable51 {52 int[] defaultArrayForPrimitiveType =53 (int[])action.invoke(invocationReturning(int[].class));54 assertEquals("should be empty array", 0, defaultArrayForPrimitiveType.length);55 Object[] defaultArrayForReferenceType =56 (Object[])action.invoke(invocationReturning(Object[].class));57 assertEquals("should be empty array", 0, defaultArrayForReferenceType.length);58 }59 public interface InterfaceType {60 int returnInt();61 }62 // Inspired by http://www.c2.com/cgi/wiki?JavaNullProxy63 public void testIfImposteriserCanImposteriseReturnTypeReturnsNewMockObjectWithSameReturnDefaultValueAction() throws Throwable {64 Imposteriser imposteriser = new JavaReflectionImposteriser() {65 @Override66 public boolean canImposterise(Class<?> c) {67 return c == InterfaceType.class;68 }69 };70 71 action = new ReturnDefaultValueAction(imposteriser);72 73 int intResult = -1;74 75 action.addResult(int.class, new Integer(intResult));76 77 InterfaceType result = (InterfaceType)action.invoke(invocationReturning(InterfaceType.class));78 79 assertEquals("int result from 'null' interface implementation",80 intResult, result.returnInt());81 82 assertEquals("should not have returned a mock Runnable because the imposteriser cannot imposterise it",83 null, action.invoke(invocationReturning(Runnable.class)));84 }85 86 public void testDefaultResultsCanBeExplicitlyOverriddenByType() throws Throwable {87 int newDefaultIntResult = 20;88 String newDefaultStringResult = "hello";89 action.addResult(String.class, newDefaultStringResult);90 action.addResult(int.class, new Integer(newDefaultIntResult));91 assertEquals("expected registered value for string result type",92 newDefaultStringResult, action.invoke(invocationReturning(String.class)));93 assertEquals("expected registered value for int result type",94 new Integer(newDefaultIntResult), action.invoke(invocationReturning(int.class)));95 }96 public void testAnExplicitlyRegisteredResultOverridesThePreviousResultForTheSameType() throws Throwable {97 action.addResult(String.class, "result1");98 action.addResult(String.class, "result2");99 assertEquals("expected second result",100 "result2", action.invoke(invocationReturning(String.class)));101 }102 class UnsupportedReturnType103 {104 }105 public void testInvocationWithAnUnsupportedReturnTypeReturnsNull()106 throws Throwable107 {108 Class<?> unsupportedReturnType = UnsupportedReturnType.class;109 Object result = action.invoke(invocationReturning(unsupportedReturnType));110 111 assertNull("should have returned null", result);112 }113 114 public void assertHasRegisteredValue(ReturnDefaultValueAction action,115 Class<?> resultType,116 Object resultValue )117 throws Throwable118 {119 assertEquals("expected " + resultValue + " to be returned",120 resultValue, action.invoke(invocationReturning(resultType)));121 }122 public void assertHasNotRegisteredReturnType( ReturnDefaultValueAction action,123 Class<?> resultType )124 throws Throwable125 {126 try {127 action.invoke(invocationReturning(resultType));128 fail("action should not support return type " + resultType);129 }130 catch (AssertionFailedError expected) {131 return;132 }133 }134 private Invocation invocationReturning(Class<?> resultType) {135 return new Invocation("INVOKED-OBJECT",136 METHOD_FACTORY.newMethodReturning(resultType),...
Source:Smock.java
...7import org.jmock.api.ExpectationError;8import org.jmock.internal.ExpectationBuilder;9import org.jmock.internal.ExpectationCollector;10import org.jmock.internal.InvocationDispatcher;11import org.jmock.internal.ReturnDefaultValueAction;12import org.jmock.lib.JavaReflectionImposteriser;13import org.objectweb.asm.Type;14import edu.mit.csail.pag.amock.util.ClassName;15public class Smock {16 public static InvocationDispatcher dispatcher;17 // This is used to make primitive-returning functions return 0,18 // object-returning functions null, etc.19 private static final Action returnifier20 = new ReturnDefaultValueAction(new JavaReflectionImposteriser());21 22 public static Result maybeMockStaticMethod(String classNameSlashed,23 String name,24 String desc,25 Object[] args) throws Throwable {26 if (dispatcher == null) {27 return new Result(false, null);28 }29 ClassName className = ClassName.fromSlashed(classNameSlashed);30 Class<?> theClass = getGuaranteedClass(className);31 CapturingClass capturedClass = CapturingClass.getCapturingClass(theClass);32 Type[] argTypes = Type.getArgumentTypes(desc);33 Class[] argClasses = new Class[argTypes.length];34 for (int i = 0; i < argTypes.length; i++) {...
Source:TestWithMocks.java
...3import org.jmock.Mockery;4import org.jmock.Sequence;5import org.jmock.api.Invocation;6import org.jmock.integration.junit4.JUnit4Mockery;7import org.jmock.internal.ReturnDefaultValueAction;8import org.junit.After;910import basis.util.concurrent.Latch;11121314public abstract class TestWithMocks extends CleanTestBase {1516 public class Expectations extends org.jmock.Expectations {17 private final Sequence defaultSequence = _mockery.sequence("Default Sequence");18 19 /** Used to indicate methods have to be called in the correct sequence. */20 protected void inSequence() {21 inSequence(defaultSequence);22 }2324 protected void willOpen(final Latch latch) {25 will(new ReturnDefaultValueAction() { @Override public Object invoke(Invocation invocation) throws Throwable {26 latch.open();27 return super.invoke(invocation);28 }});29 }30 }3132 protected TestWithMocks() {33 super();34 }35 36 private final Mockery _mockery = new JUnit4Mockery();3738 39 @After
...
ReturnDefaultValueAction
Using AI Code Generation
1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.core.Invocation;4import org.jmock.core.InvocationMatcher;5import org.jmock.core.Stub;6import org.jmock.core.constraint.IsAnything;7import org.jmock.core.constraint.IsEqual;8import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;9import org.jmock.core.matcher.InvokeOnceMatcher;10import org.jmock.core.stub.ReturnDefaultValueAction;11import org.jmock.core.stub.ReturnStub;12import org.jmock.core.stub.ThrowStub;13import org.jmock.core.stub.VoidStub;14public class TestMock extends MockObjectTestCase {15 public void testMock() {16 Mock mock = new Mock(Interface.class);17 mock.expects(new InvokeOnceMatcher()).method("method").with(18 new IsEqual("arg")).will(new ReturnStub("return value"));19 Interface objectToTest = (Interface) mock.proxy();20 assertEquals("return value", objectToTest.method("arg"));21 }22}23import org.jmock.Mock;24import org.jmock.MockObjectTestCase;25import org.jmock.core.Invocation;26import org.jmock.core.InvocationMatcher;27import org.jmock.core.Stub;28import org.jmock.core.constraint.IsAnything;29import org.jmock.core.constraint.IsEqual;30import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;31import org.jmock.core.matcher.InvokeOnceMatcher;32import org.jmock.core.stub.ReturnDefaultValueAction;33import org.jmock.core.stub.ReturnStub;34import org.jmock.core.stub.ThrowStub;35import org.jmock.core.stub.VoidStub;36public class TestMock extends MockObjectTestCase {37 public void testMock() {38 Mock mock = new Mock(Interface.class);39 mock.expects(new InvokeOnceMatcher()).method("method").with(40 new IsEqual("arg")).will(new ReturnStub("return value"));41 Interface objectToTest = (Interface) mock.proxy();42 assertEquals("return value", objectToTest.method("arg"));43 }44}
ReturnDefaultValueAction
Using AI Code Generation
1import org.jmock.MockObjectTestCase;2import org.jmock.core.Invocation;3import org.jmock.core.InvocationMatcher;4import org.jmock.core.Stub;5import org.jmock.core.stub.ReturnDefaultValueAction;6import org.jmock.core.matcher.InvokeOnceMatcher;7import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;8import org.jmock.core.matcher.InvokeAtMostOnceMatcher;9import org.jmock.core.matcher.InvokeAtLeastCountMatcher;10import org.jmock.core.matcher.InvokeAtMostCountMatcher;11import org.jmock.core.matcher.InvokeCountMatcher;12import org.jmock.core.matcher.InvokeBetweenCountMatcher;13import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;14import org.jmock.core.matcher.InvokeAtMostOnceMatcher;15import org.jmock.core.matcher.InvokeAtLeastCountMatcher;16import org.jmock.core.matcher.InvokeAtMostCountMatcher;17import org.jmock.core.matcher.InvokeCountMatcher;18import org.jmock.core.matcher.InvokeBetweenCountMatcher;19import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;20import org.jmock.core.matcher.InvokeAtMostOnceMatcher;21import org.jmock.core.matcher.InvokeAtLeastCountMatcher;22import org.jmock.core.matcher.InvokeAtMostCountMatcher;23import org.jmock.core.matcher.InvokeCountMatcher;24import org.jmock.core.matcher.InvokeBetweenCountMatcher;25public class TestReturnDefaultValueAction extends MockObjectTestCase {26 public void testDefaultActionForInt() {27 Invocation invocation = new Invocation("mock", "method", new Class[0], new Object[0], null);28 Stub stub = new ReturnDefaultValueAction();29 assertEquals(0, stub.invoke(invocation));30 }31 public void testDefaultActionForBoolean() {32 Invocation invocation = new Invocation("mock", "method", new Class[0], new Object[0], null);33 Stub stub = new ReturnDefaultValueAction();34 assertEquals(false, stub.invoke(invocation));35 }36 public void testDefaultActionForObject() {37 Invocation invocation = new Invocation("mock", "method", new Class[0], new Object[0], null);38 Stub stub = new ReturnDefaultValueAction();39 assertEquals(null, stub.invoke(invocation));40 }41 public void testDefaultActionForString() {42 Invocation invocation = new Invocation("mock", "method", new Class[0], new Object[0], null);43 Stub stub = new ReturnDefaultValueAction();44 assertEquals(null, stub.invoke(invocation));45 }46 public void testDefaultActionForLong() {
ReturnDefaultValueAction
Using AI Code Generation
1import org.jmock.core.Invocation;2import org.jmock.core.Stub;3import org.jmock.core.StubAction;4import org.jmock.core.StubAction;5import org.jmock.core.stub.ReturnDefaultValueAction;6import org.jmock.core.stub.ReturnStub;
ReturnDefaultValueAction
Using AI Code Generation
1package com.jmock;2import org.jmock.Mockery;3import org.jmock.States;4import org.jmock.api.Action;5import org.jmock.api.Invocation;6import org.jmock.lib.action.ReturnDefaultValueAction;7import org.jmock.lib.action.ReturnValueAction;8import org.jmock.lib.action.ThrowAction;9import org.jmock.lib.action.VoidAction;10import org.junit.Test;11public class JmockTest {12public static void main(String[] args) {13}14public void test1() {15Mockery context = new Mockery();16States state = context.states("state");17state.is("one");18final Action action1 = new ReturnDefaultValueAction();19final Action action2 = new ReturnValueAction("value2");20final Action action3 = new ThrowAction(new RuntimeException());21final Action action4 = new VoidAction();22context.checking(new Expectations() {23{24allowing(mock).method();25will(action1);26}27});28}29}30Exception in thread "main" java.lang.NoSuchMethodError: org.jmock.Mockery.checking(Lorg/jmock/api/ExpectationBuilder;)V31at com.jmock.JmockTest.test1(JmockTest.java:39)32at com.jmock.JmockTest.main(JmockTest.java:19)33Exception in thread "main" java.lang.NoSuchMethodError: org.jmock.Mockery.checking(Lorg/jmock/api/ExpectationBuilder;)V34at com.jmock.JmockTest.test1(JmockTest.java:39)35at com.jmock.JmockTest.main(JmockTest.java:19)36Exception in thread "main" java.lang.NoSuchMethodError: org.jmock.Mockery.checking(Lorg/jmock/api/ExpectationBuilder;)V37at com.jmock.JmockTest.test1(JmockTest.java:39)38at com.jmock.JmockTest.main(JmockTest.java:19)39Exception in thread "main" java.lang.NoSuchMethodError: org.jmock.Mockery.checking(Lorg/jmock/api/ExpectationBuilder;)V40at com.jmock.JmockTest.test1(JmockTest.java:39)41at com.jmock.JmockTest.main(JmockTest.java:19)42Exception in thread "main" java.lang.NoSuchMethodError: org.jmock.Mockery.checking(Lorg/jmock/api/ExpectationBuilder;)V43at com.jmock.JmockTest.test1(JmockTest.java:39)
ReturnDefaultValueAction
Using AI Code Generation
1package org.jmock.internal;2import org.jmock.api.Action;3import org.jmock.api.Invocation;4public class ReturnDefaultValueAction implements Action {5 private static final long serialVersionUID = 1L;6 private final Object returnValue;7 public ReturnDefaultValueAction(Class<?> type) {8 this.returnValue = createDefaultValue(type);9 }10 public Object invoke(Invocation invocation) throws Throwable {11 return returnValue;12 }13 public void describeTo(Description description) {14 description.appendText("return default value for type ");15 description.appendValue(returnValue.getClass());16 }17 private Object createDefaultValue(Class<?> type) {18 if (type.isPrimitive()) {19 if (type == boolean.class) {20 return false;21 }22 return 0;23 }24 return null;25 }26}27package org.jmock.internal;28import org.jmock.api.Action;29import org.jmock.api.Invocation;30public class ReturnDefaultValueAction implements Action {31 private static final long serialVersionUID = 1L;32 private final Object returnValue;33 public ReturnDefaultValueAction(Class<?> type) {34 this.returnValue = createDefaultValue(type);35 }36 public Object invoke(Invocation invocation) throws Throwable {37 return returnValue;38 }39 public void describeTo(Description description) {40 description.appendText("return default value for type ");41 description.appendValue(returnValue.getClass());42 }43 private Object createDefaultValue(Class<?> type) {44 if (type.isPrimitive()) {45 if (type == boolean.class) {46 return false;47 }48 return 0;49 }50 return null;51 }52}53package org.jmock.internal;54import org.jmock.api.Action;55import org.jmock.api.Invocation;56public class ReturnDefaultValueAction implements Action {57 private static final long serialVersionUID = 1L;58 private final Object returnValue;59 public ReturnDefaultValueAction(Class<?> type) {60 this.returnValue = createDefaultValue(type);61 }62 public Object invoke(Invocation invocation) throws Throwable {63 return returnValue;64 }65 public void describeTo(Description description) {66 description.appendText("return default value for type ");67 description.appendValue(returnValue.getClass());68 }69 private Object createDefaultValue(Class<?> type) {70 if (type.isPrimitive()) {71 if (type == boolean.class) {72 return false;
ReturnDefaultValueAction
Using AI Code Generation
1import org.jmock.Expectations;2import org.jmock.Mockery;3import org.jmock.Sequence;4import org.jmock.integration.junit4.JUnit4Mockery;5import org.jmock.internal.ReturnDefaultValueAction;6import org.junit.Test;7public class Test1 {8 private final Mockery context = new JUnit4Mockery();9 public void test() {10 final Interface1 mock = context.mock(Interface1.class, new ReturnDefaultValueAction());11 context.checking(new Expectations() {12 {13 oneOf(mock).method1();14 will(returnValue(1));15 oneOf(mock).method2();16 will(returnValue(2));17 }18 });19 System.out.println(mock.method1());20 System.out.println(mock.method2());21 System.out.println(mock.method3());22 }23 public interface Interface1 {24 int method1();25 int method2();26 int method3();27 }28}29import org.jmock.Expectations;30import org.jmock.Mockery;31import org.jmock.Sequence;32import org.jmock.integration.junit4.JUnit4Mockery;33import org.jmock.internal.ReturnDefaultValueAction;34import org.junit.Test;35public class Test2 {36 private final Mockery context = new JUnit4Mockery();37 public void test() {38 final Interface1 mock = context.mock(Interface1.class, new ReturnDefaultValueAction());39 context.checking(new Expectations() {40 {41 oneOf(mock).method1();42 will(returnValue(1));43 oneOf(mock).method2();44 will(returnValue(2));45 }46 });47 System.out.println(mock.method1());48 System.out.println(mock.method2());49 System.out.println(mock.method3());50 }51 public interface Interface1 {52 int method1();53 int method2();54 int method3();55 }56}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!