Best Jmock-library code snippet using org.jmock.test.unit.api.InvocationTests.toString
Source:InvocationTests.java
...8import org.jmock.test.unit.support.AssertThat;9import org.jmock.test.unit.support.MethodFactory;10public class InvocationTests extends TestCase {11 final Object INVOKED = new Object() {12 @Override public String toString() { return "INVOKED"; }13 };14 final String METHOD_NAME = "methodName";15 final Class<?>[] ARG_TYPES = new Class[]{int.class, boolean.class};16 final Class<?> RETURN_TYPE = String.class;17 final Object[] ARG_VALUES = {new Integer(0), Boolean.TRUE};18 final Class<?>[] EXCEPTION_TYPES = new Class[]{InterruptedException.class, SecurityException.class};19 MethodFactory methodFactory;20 Method method;21 public InvocationTests( String name ) {22 super(name);23 }24 @Override25 public void setUp() throws Exception {26 methodFactory = new MethodFactory();27 method = methodFactory.newMethod(METHOD_NAME, ARG_TYPES, RETURN_TYPE, EXCEPTION_TYPES);28 }29 public void testCanBeConstructedFromAMethodObject() throws Exception {30 Invocation invocation = new Invocation(INVOKED, method, ARG_VALUES);31 assertSame("invoked object", INVOKED, invocation.getInvokedObject());32 assertEquals("invoked method", method, invocation.getInvokedMethod());33 assertEquals("name", method.getName(), invocation.getInvokedMethod().getName());34 assertEquals("parameter types",35 Arrays.asList(method.getParameterTypes()),36 Arrays.asList(invocation.getInvokedMethod().getParameterTypes()));37 assertEquals("return type",38 method.getReturnType(), invocation.getInvokedMethod().getReturnType());39 assertEquals("parameter count", ARG_VALUES.length, invocation.getParameterCount());40 assertEquals("parameter values",41 Arrays.asList(ARG_VALUES), Arrays.asList(invocation.getParametersAsArray()));42 }43 public void testConstructorInterpretsNullParameterValueArrayAsZeroArguments() {44 Invocation invocation = new Invocation(INVOKED, method);45 assertEquals("expected no parameters values",46 0, invocation.getParameterCount());47 }48 public void testTestsForEqualityOnTargetAndMethodSignatureAndArguments() {49 Invocation invocation1 = new Invocation(INVOKED, method, ARG_VALUES);50 Invocation invocation2 = new Invocation(INVOKED, method, ARG_VALUES);51 Invocation differentTarget = new Invocation("OTHER TARGET", method, ARG_VALUES);52 Invocation differentMethod = new Invocation(INVOKED,53 methodFactory.newMethod("OTHER_" + METHOD_NAME, ARG_TYPES, RETURN_TYPE, EXCEPTION_TYPES),54 ARG_VALUES);55 Invocation differentArgValues = new Invocation(INVOKED, method,56 new Object[]{new Integer(1), Boolean.FALSE});57 assertTrue("should be equal to itself", invocation1.equals(invocation1));58 assertTrue("identical calls should be equal", invocation1.equals(invocation2));59 assertFalse("should not be equal to object that is not an Invocation",60 invocation1.equals(new Object()));61 assertFalse("should not be equal to null",62 invocation1.equals(null));63 assertFalse("should not be equal if different invoked object",64 invocation1.equals(differentTarget));65 assertFalse("should not be equal if different method",66 invocation1.equals(differentMethod));67 assertFalse("should not be equal if different argumentValues",68 invocation1.equals(differentArgValues));69 }70 public void testFollowsEqualsHashcodeProtocol() {71 Invocation invocation1 = new Invocation(INVOKED, method, ARG_VALUES);72 Invocation invocation2 = new Invocation(INVOKED, method, ARG_VALUES);73 assertEquals("should have equal hash codes",74 invocation1.hashCode(), invocation2.hashCode());75 }76 public void testToStringWithTwoArguments() throws Exception {77 Invocation invocation = new Invocation(INVOKED,78 methodFactory.newMethod(METHOD_NAME, new Class[]{String.class, String.class}, void.class,79 EXCEPTION_TYPES),80 new Object[]{"arg1", "arg2"});81 String result = invocation.toString();82 AssertThat.stringIncludes("Should contain object name", INVOKED.toString(), result);83 AssertThat.stringIncludes("Should contain method name", METHOD_NAME, result);84 AssertThat.stringIncludes("Should contain firstArg", "arg1", result);85 AssertThat.stringIncludes("Should contain second Arg", "arg2", result);86 }87 public void testToStringWithStringArray() throws Exception {88 Invocation invocation = new Invocation(INVOKED,89 methodFactory.newMethod(METHOD_NAME, new Class[]{String[].class}, void.class, EXCEPTION_TYPES),90 new Object[]{new String[]{"arg1", "arg2"}});91 String result = invocation.toString();92 AssertThat.stringIncludes("Should contain method name", METHOD_NAME, result);93 AssertThat.stringIncludes("Should contain args as an array", "[\"arg1\", \"arg2\"]", result);94 }95 public void testToStringWithPrimitiveArray() throws Exception {96 Invocation invocation = new Invocation(INVOKED,97 methodFactory.newMethod(METHOD_NAME, new Class[]{long[].class}, void.class, EXCEPTION_TYPES),98 new Object[]{new long[]{1, 2}});99 String result = invocation.toString();100 AssertThat.stringIncludes("Should contain invokedMethod name", METHOD_NAME, result);101 AssertThat.stringIncludes("Should contain args as an array", "[<1L>, <2L>]", result);102 }103 public void testMethodToStringWithNullArg() throws Exception {104 Invocation invocation = new Invocation(INVOKED, methodFactory.newMethod(METHOD_NAME, new Class[]{String.class}, void.class, EXCEPTION_TYPES),105 new Object[]{null});106 String result = invocation.toString();107 AssertThat.stringIncludes("Should contain invokedMethod name", METHOD_NAME, result);108 AssertThat.stringIncludes("Should contain firstArg", "null", result);109 }110 public void testMethodToStringWithObjectArg() throws Exception {111 final String argAsString = "TO_STRING_RESULT";112 Object arg = new Object()113 {114 @Override public String toString() {115 return argAsString;116 }117 };118 Invocation invocation = new Invocation(INVOKED, methodFactory.newMethod(METHOD_NAME, new Class[]{String.class}, void.class, EXCEPTION_TYPES),119 new Object[]{arg});120 String result = invocation.toString();121 AssertThat.stringIncludes("Should contain invokedMethod name", METHOD_NAME, result);122 AssertThat.stringIncludes("Should contain firstArg", argAsString, result);123 }124 public void testReturnTypeCheckFailsIfReturningValueFromVoidMethod() {125 Invocation invocation = 126 new Invocation(INVOKED, methodFactory.newMethodReturning(void.class));127 128 try {129 invocation.checkReturnTypeCompatibility("string result");130 }131 catch (IllegalStateException ex) {132 AssertThat.stringIncludes("should describe error",133 "tried to return a value from a void method", ex.getMessage());134 return;135 }136 fail("should have failed");137 }138 139 public void testReturnTypeCheckFailsIfReturnedValueIsIncompatible() {140 Invocation invocation = 141 new Invocation(INVOKED, methodFactory.newMethodReturning(int.class));142 try {143 invocation.checkReturnTypeCompatibility("string result");144 }145 catch (IllegalStateException ex) {146 AssertThat.stringIncludes("expected return type", int.class.toString(), ex.getMessage());147 AssertThat.stringIncludes("returned value type", String.class.getName(), ex.getMessage());148 return;149 }150 fail("should have failed");151 }152 public void testReturnTypeCheckFailsWhenReturningNullFromMethodWithPrimitiveReturnType() {153 Invocation invocation = 154 new Invocation(INVOKED, methodFactory.newMethodReturning(int.class));155 156 try {157 invocation.checkReturnTypeCompatibility(null);158 }159 catch (IllegalStateException ex) {160 AssertThat.stringIncludes("expected return type", int.class.toString(), ex.getMessage());161 AssertThat.stringIncludes("null", String.valueOf((Object)null), ex.getMessage());162 return;163 }164 fail("should have failed");165 }166 public void testReturnTypeCheckAllowsReturningBoxedTypeFromMethodWithPrimitiveReturnType() {167 Invocation invocation = 168 new Invocation(INVOKED, methodFactory.newMethodReturning(int.class));169 170 invocation.checkReturnTypeCompatibility(new Integer(0));171 }172 public void testReturnTypeCheckAllowsReturningNullFromMethodWithNonPrimitiveReturnType() {173 Invocation invocation = 174 new Invocation(INVOKED, methodFactory.newMethodReturning(String.class));...
toString
Using AI Code Generation
1import org.jmock.test.unit.support.InvocationMatcherBuilder2import org.jmock.integration.junit4.JUnitRuleMockery3import org.jmock.api.Action4import org.jmock.api.Invocation5import org.jmock.api.Invokable6import org.jmock.api.ExpectationError7import org.jmock.api.Expectation8import org.hamcrest.Matcher9import org.hamcrest.Description10import org.hamcrest.StringDescription11import org.hamcrest.SelfDescribing12import org.junit.Rule13import org.junit.Test14import org.junit.rules.TestRule15import org.hamcrest.MatcherAssert.assertThat16import org.hamcrest.Matchers.equalTo17import org.hamcrest.Matchers.is18import org.hamcrest.Matchers.sameInstance19import org.hamcrest.Matchers.nullValue20import org.hamcrest.Matchers.containsString21import org.hamcrest.Matchers.not22import org.hamcrest.Matchers.hasToString23import org.hamcrest.Matchers.instanceOf24import org.hamcrest.Matchers.greaterThan25import org.hamcrest.Matchers.lessThan26import org.hamcrest.Matchers.hasItem27import org.hamcrest.Matchers.hasItems28import org.hamcrest.Matchers.hasProperty29import org.hamcrest.Matchers.hasEntry30import org.hamcrest.Matchers.hasKey31import org.hamcrest.Matchers.hasValue32import org.hamcrest.Matchers.not33import org.hamcrest.Matchers.both34import org.hamcrest.Matchers.either35import org.hamcrest.Matchers.anyOf36import org.hamcrest.Matchers.anything37import org.hamcrest.Matchers.describedAs38import org.hamcrest.Matchers.not39import org.hamcrest.Matchers.anything40import org.hamcrest.Matchers.is41import org.hamcrest.Matchers.i
toString
Using AI Code Generation
1import org.jmock.api.Invocation2import org.jmock.api.Invokable3import org.jmock.api.Action4import org.jmock.api.Expectation5import org.jmock.api.Imposteriser6import org.jmock.api.InvocationDispatcher7import org.jmock.api.InvocationHandler8import org.jmock.api.InvocationExpectation9import org.jmock.api.InvocationExpectationBuilder10import org.jmock.api.InvocationMatcher11import org.jmock.api.InvocationName12import org.jmock.api.InvocationRecorder13import org.jmock.api.InvocationResult14import org.jmock.api.InvocationSequence15import org.jmock.api.InvocationStubber16import org.jmock.api.MockObjectNamingScheme17import org.jmock.api.Mockery18import org.jmock.api.StatePredicate19import org.jmock.api.ThreadSafeMockery20import org.jmock.api.Verification21import org.jmock.api.VerificationMode22import org.jmock.api.WithExpectations23import org.jmock.api.action.ActionSequence24import org.jmock.api.action.CustomAction25import org.jmock.api.action.DoAll26import org.jmock.api.action.DoActions27import org.jmock.api.action.DoNothing28import org.jmock.api.action.DoReturn29import org.jmock.api.action.DoThrow30import org.jmock.api.action.Invoke31import org.jmock.api.action.InvokeAction32import org.jmock.api.action.InvokeActionWithParams33import org.jmock.api.action.InvokeFunction34import org.jmock.api.action.InvokeFunctionWithParams35import org.jmock.api.action.ReturnValueAction36import org.jmock.api.action.ThrowExceptionAction37import org.jmock.api.legacy.ClassImposteriser38import org.jmock.api.legacy.Imposteriser39import org.jmock.api.legacy.ImposteriserSelector40import org.jmock.api.legacy.LegacyImposteriser41import org.jmock.api.legacy.LegacyImposteriserInstance42import org.jmock.api.legacy.LegacyImposteriserSelector43import org.jmock.api.legacy.LegacyImposteriserSelectorInstance44import org.jmock.api.legacy.LegacyMockery45import org.jmock.api.legacy.LegacyThreadSafeMockery46import org.jmock.api.legacy.MockeryInstance47import org.jmock.api.legacy.MockeryInstanceBuilder48import org.jmock.api.legacy.MockeryInstanceBuilderWithImposteriser
toString
Using AI Code Generation
1import org.jmock.test.unit.api.InvocationTests;2import org.jmock.test.unit.api.Invocation;3import org.junit.Test;4import static org.junit.Assert.assertEquals;5public class InvocationTest {6 public void testToString() {7 Invocation invocation = new InvocationTests().new InvocationImpl("method", new Object[] { "arg1", "arg2" });8 assertEquals("method(\"arg1\", \"arg2\")", invocation.toString());9 }10}11method("arg1", "arg2")
toString
Using AI Code Generation
1import org.jmock.api.Invocation2import org.jmock.test.unit.api.InvocationTests3def "toString method of Invocation class"() {4 new Invocation(method, arguments).toString() == InvocationTests.toString(method, arguments)5}6def "equals method of Invocation class"() {7 new Invocation(method, arguments) == new Invocation(method, arguments)8}9def "Invocation class is serializable"() {10 new Invocation(method, arguments) instanceof Serializable11}12def method = getClass().getDeclaredMethod("toString")
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!!