Best Powermock code snippet using samples.expectnew.Target
Source:ExpectNewCases.java
...11import samples.Service;12import samples.expectnew.CreationException;13import samples.expectnew.ExpectNewDemo;14import samples.expectnew.ExpectNewServiceUser;15import samples.expectnew.ITarget;16import samples.expectnew.Target;17import samples.expectnew.VarArgsConstructorDemo;18import samples.newmocking.MyClass;19public abstract class ExpectNewCases {20 public static final String TARGET_NAME = "MyTarget";21 public static final int TARGET_ID = 1;22 public static final String UNKNOWN_TARGET_NAME = "Unknown2";23 public static final int UNKNOWN_TARGET_ID = -11;24 @Test25 public void testNewWithCheckedException() throws Exception {26 ExpectNewDemo tested = new ExpectNewDemo();27 final String expectedFailMessage = "testing checked exception";28 PowerMock.expectNew(MyClass.class).andThrow(new IOException(expectedFailMessage));29 PowerMock.replay(MyClass.class);30 try {31 tested.throwExceptionAndWrapInRunTimeWhenInvoction();32 Assert.fail("Should throw a checked Exception!");33 } catch (RuntimeException e) {34 Assert.assertTrue(((e.getCause()) instanceof IOException));35 Assert.assertEquals(expectedFailMessage, e.getMessage());36 }37 PowerMock.verify(MyClass.class);38 }39 @Test40 public void testGetMessage() throws Exception {41 ExpectNewDemo tested = new ExpectNewDemo();42 MyClass myClassMock = PowerMock.createMock(MyClass.class);43 PowerMock.expectNew(MyClass.class).andReturn(myClassMock);44 String expected = "Hello altered World";45 expect(myClassMock.getMessage()).andReturn("Hello altered World");46 PowerMock.replay(myClassMock, MyClass.class);47 String actual = tested.getMessage();48 PowerMock.verify(myClassMock, MyClass.class);49 Assert.assertEquals("Expected and actual did not match", expected, actual);50 }51 @Test52 public void testGetMessageWithArgument() throws Exception {53 ExpectNewDemo tested = new ExpectNewDemo();54 MyClass myClassMock = PowerMock.createMock(MyClass.class);55 PowerMock.expectNew(MyClass.class).andReturn(myClassMock);56 String expected = "Hello altered World";57 expect(myClassMock.getMessage("test")).andReturn("Hello altered World");58 PowerMock.replay(myClassMock, MyClass.class);59 String actual = tested.getMessageWithArgument();60 PowerMock.verify(myClassMock, MyClass.class);61 Assert.assertEquals("Expected and actual did not match", expected, actual);62 }63 @Test64 public void testInvokeVoidMethod() throws Exception {65 ExpectNewDemo tested = new ExpectNewDemo();66 MyClass myClassMock = PowerMock.createMock(MyClass.class);67 PowerMock.expectNew(MyClass.class).andReturn(myClassMock);68 myClassMock.voidMethod();69 expectLastCall().times(1);70 PowerMock.replay(myClassMock, MyClass.class);71 tested.invokeVoidMethod();72 PowerMock.verify(myClassMock, MyClass.class);73 }74 @Test75 public void testNewWithRuntimeException() throws Exception {76 ExpectNewDemo tested = new ExpectNewDemo();77 final String expectedFailMessage = "testing";78 PowerMock.expectNew(MyClass.class).andThrow(new RuntimeException(expectedFailMessage));79 PowerMock.replay(MyClass.class);80 try {81 tested.throwExceptionWhenInvoction();82 Assert.fail("Should throw RuntimeException!");83 } catch (RuntimeException e) {84 Assert.assertEquals(expectedFailMessage, e.getMessage());85 }86 PowerMock.verify(MyClass.class);87 }88 @Test89 public void testPreviousProblemsWithByteCodeManipulation() throws Exception {90 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);91 expect(myClassMock1.getMessage()).andReturn("Hello");92 expect(myClassMock1.getMessage()).andReturn("World");93 PowerMock.replay(myClassMock1);94 Assert.assertEquals("Hello", myClassMock1.getMessage());95 Assert.assertEquals("World", myClassMock1.getMessage());96 PowerMock.verify(myClassMock1);97 }98 @Test99 public void testMultipleNew() throws Exception {100 ExpectNewDemo tested = new ExpectNewDemo();101 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);102 MyClass myClassMock2 = PowerMock.createMock(MyClass.class);103 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1);104 PowerMock.expectNew(MyClass.class).andReturn(myClassMock2);105 expect(myClassMock1.getMessage()).andReturn("Hello ");106 expect(myClassMock2.getMessage()).andReturn("World");107 PowerMock.replay(myClassMock1, myClassMock2, MyClass.class);108 final String actual = tested.multipleNew();109 PowerMock.verify(myClassMock1, myClassMock2, MyClass.class);110 Assert.assertEquals("Hello World", actual);111 }112 @Test113 public void testSimpleMultipleNew() throws Exception {114 ExpectNewDemo tested = new ExpectNewDemo();115 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);116 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).times(3);117 PowerMock.replay(myClassMock1, MyClass.class);118 tested.simpleMultipleNew();119 PowerMock.verify(myClassMock1, MyClass.class);120 }121 @Test122 public void testSimpleMultipleNew_tooManyTimesExpected() throws Exception {123 ExpectNewDemo tested = new ExpectNewDemo();124 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);125 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).times(4);126 PowerMock.replay(myClassMock1, MyClass.class);127 tested.simpleMultipleNew();128 try {129 PowerMock.verify(myClassMock1, MyClass.class);130 Assert.fail("Should throw AssertionError.");131 } catch (AssertionError e) {132 Assert.assertEquals(("\n Expectation failure on verify:" + "\n samples.newmocking.MyClass(): expected: 4, actual: 3"), e.getMessage());133 }134 }135 @Test136 public void testSimpleMultipleNew_tooFewTimesExpected() throws Exception {137 ExpectNewDemo tested = new ExpectNewDemo();138 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);139 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).times(2);140 PowerMock.replay(myClassMock1, MyClass.class);141 try {142 tested.simpleMultipleNew();143 Assert.fail("Should throw AssertionError.");144 } catch (AssertionError e) {145 Assert.assertEquals(("\n Unexpected constructor call samples.newmocking.MyClass():" + "\n samples.newmocking.MyClass(): expected: 2, actual: 3"), e.getMessage());146 }147 }148 /**149 * Verifies that the issue150 * http://code.google.com/p/powermock/issues/detail?id=10 is solved.151 */152 @Test153 public void testSimpleMultipleNewPrivate_tooFewTimesExpected() throws Exception {154 ExpectNewDemo tested = new ExpectNewDemo();155 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);156 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).times(2);157 PowerMock.replay(myClassMock1, MyClass.class);158 try {159 Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");160 Assert.fail("Should throw AssertionError.");161 } catch (AssertionError e) {162 Assert.assertEquals(("\n Unexpected constructor call samples.newmocking.MyClass():" + "\n samples.newmocking.MyClass(): expected: 2, actual: 3"), e.getMessage());163 }164 }165 @Test166 public void testSimpleMultipleNewPrivate_ok() throws Exception {167 ExpectNewDemo tested = new ExpectNewDemo();168 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);169 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).times(3);170 PowerMock.replay(myClassMock1, MyClass.class);171 Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");172 }173 @Test174 public void testSimpleSingleNew_withOnce() throws Exception {175 ExpectNewDemo tested = new ExpectNewDemo();176 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);177 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).once();178 PowerMock.replay(myClassMock1, MyClass.class);179 tested.simpleSingleNew();180 PowerMock.verify(myClassMock1, MyClass.class);181 }182 @Test183 public void testSimpleSingleNew_withAtLeastOnce() throws Exception {184 ExpectNewDemo tested = new ExpectNewDemo();185 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);186 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).atLeastOnce();187 PowerMock.replay(myClassMock1, MyClass.class);188 tested.simpleSingleNew();189 PowerMock.verify(myClassMock1, MyClass.class);190 }191 @Test192 public void testSimpleMultipleNew_withAtLeastOnce() throws Exception {193 ExpectNewDemo tested = new ExpectNewDemo();194 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);195 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).atLeastOnce();196 PowerMock.replay(myClassMock1, MyClass.class);197 tested.simpleMultipleNew();198 PowerMock.verify(myClassMock1, MyClass.class);199 }200 @Test201 public void testSimpleMultipleNew_withRange_lowerBoundLessThan0() throws Exception {202 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);203 try {204 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).times((-20), 2);205 Assert.fail("Should throw IllegalArgumentException.");206 } catch (IllegalArgumentException e) {207 Assert.assertEquals("minimum must be >= 0", e.getMessage());208 }209 }210 @Test211 public void testSimpleMultipleNew_withRange_upperBoundLessThan0() throws Exception {212 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);213 try {214 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).times((-1), (-2));215 Assert.fail("Should throw IllegalArgumentException.");216 } catch (IllegalArgumentException e) {217 Assert.assertTrue(e.getMessage().contains("<="));218 }219 }220 @Test221 public void testSimpleMultipleNew_withRange_upperBoundLessThanLowerBound() throws Exception {222 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);223 try {224 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).times(10, 2);225 Assert.fail("Should throw IllegalArgumentException.");226 } catch (IllegalArgumentException e) {227 Assert.assertTrue(e.getMessage().contains("<="));228 }229 }230 @Test231 public void testSimpleMultipleNew_withRange_OK() throws Exception {232 ExpectNewDemo tested = new ExpectNewDemo();233 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);234 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).times(1, 5);235 PowerMock.replay(myClassMock1, MyClass.class);236 tested.simpleMultipleNew();237 PowerMock.verify(myClassMock1, MyClass.class);238 }239 @Test240 public void testSimpleMultipleNew_anyTimes() throws Exception {241 ExpectNewDemo tested = new ExpectNewDemo();242 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);243 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).anyTimes();244 PowerMock.replay(myClassMock1, MyClass.class);245 tested.simpleMultipleNew();246 PowerMock.verify(myClassMock1, MyClass.class);247 }248 @Test249 public void testSimpleMultipleNew_withRange_notWithinRange() throws Exception {250 ExpectNewDemo tested = new ExpectNewDemo();251 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);252 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).times(5, 7);253 PowerMock.replay(myClassMock1, MyClass.class);254 tested.simpleMultipleNew();255 try {256 PowerMock.verify(myClassMock1, MyClass.class);257 Assert.fail("Should throw AssertionError.");258 } catch (AssertionError e) {259 Assert.assertEquals(("\n Expectation failure on verify:" + "\n samples.newmocking.MyClass(): expected: between 5 and 7, actual: 3"), e.getMessage());260 }261 }262 @Test263 public void testAlternativeFlow() throws Exception {264 ExpectNewDemo tested = new ExpectNewDemo();265 PowerMock.expectNew(DataInputStream.class, new Object[]{ null }).andThrow(new RuntimeException("error"));266 PowerMock.replay(ExpectNewDemo.class, DataInputStream.class);267 InputStream stream = tested.alternativePath();268 PowerMock.verify(ExpectNewDemo.class, DataInputStream.class);269 Assert.assertNotNull("The returned inputstream should not be null.", stream);270 Assert.assertTrue("The returned inputstream should be an instance of ByteArrayInputStream.", (stream instanceof ByteArrayInputStream));271 }272 @Test273 public void testSimpleMultipleNewPrivate_tooManyTimesExpected() throws Exception {274 ExpectNewDemo tested = new ExpectNewDemo();275 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);276 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).times(4);277 PowerMock.replay(myClassMock1, MyClass.class);278 try {279 Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");280 PowerMock.verify(myClassMock1, MyClass.class);281 Assert.fail("Should throw an exception!.");282 } catch (AssertionError e) {283 Assert.assertEquals(("\n Expectation failure on verify:" + "\n samples.newmocking.MyClass(): expected: 4, actual: 3"), e.getMessage());284 }285 }286 @Test287 public void testNewWithArguments() throws Exception {288 final int numberOfTimes = 2;289 final String expected = "used";290 ExpectNewDemo tested = new ExpectNewDemo();291 ExpectNewServiceUser expectNewServiceImplMock = PowerMock.createMock(ExpectNewServiceUser.class);292 Service serviceMock = PowerMock.createMock(Service.class);293 PowerMock.expectNew(ExpectNewServiceUser.class, serviceMock, numberOfTimes).andReturn(expectNewServiceImplMock);294 expect(expectNewServiceImplMock.useService()).andReturn(expected);295 PowerMock.replay(expectNewServiceImplMock, serviceMock, ExpectNewServiceUser.class);296 Assert.assertEquals(expected, tested.newWithArguments(serviceMock, numberOfTimes));297 PowerMock.verify(expectNewServiceImplMock, serviceMock, ExpectNewServiceUser.class);298 }299 @Test300 public void testNewWithVarArgs() throws Exception {301 final String firstString = "hello";302 final String secondString = "world";303 ExpectNewDemo tested = new ExpectNewDemo();304 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);305 PowerMock.expectNew(VarArgsConstructorDemo.class, firstString, secondString).andReturn(varArgsConstructorDemoMock);306 expect(varArgsConstructorDemoMock.getAllMessages()).andReturn(new String[]{ firstString, secondString });307 PowerMock.replay(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);308 String[] varArgs = tested.newVarArgs(firstString, secondString);309 Assert.assertEquals(2, varArgs.length);310 Assert.assertEquals(firstString, varArgs[0]);311 Assert.assertEquals(secondString, varArgs[1]);312 PowerMock.verify(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);313 }314 @Test315 public void testNewWhenTheExpectedConstructorIsNotFound() throws Exception {316 final Object object = new Object();317 try {318 PowerMock.expectNew(VarArgsConstructorDemo.class, object);319 Assert.fail("Should throw ConstructorNotFoundException!");320 } catch (ConstructorNotFoundException e) {321 Assert.assertEquals((((("No constructor found in class '" + (VarArgsConstructorDemo.class.getName())) + "' with parameter types: [ ") + (object.getClass().getName())) + " ]."), e.getMessage());322 }323 }324 @Test325 public void testNewWithVarArgsConstructorWhenOneArgumentIsOfASubType() throws Exception {326 ExpectNewDemo tested = new ExpectNewDemo();327 Service serviceMock = PowerMock.createMock(Service.class);328 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);329 final Service serviceSubTypeInstance = new Service() {330 public String getServiceMessage() {331 return "message";332 }333 };334 PowerMock.expectNew(VarArgsConstructorDemo.class, serviceSubTypeInstance, serviceMock).andReturn(varArgsConstructorDemoMock);335 expect(varArgsConstructorDemoMock.getAllServices()).andReturn(new Service[]{ serviceMock });336 PowerMock.replay(serviceMock, VarArgsConstructorDemo.class, varArgsConstructorDemoMock);337 Service[] varArgs = tested.newVarArgs(serviceSubTypeInstance, serviceMock);338 Assert.assertEquals(1, varArgs.length);339 Assert.assertSame(serviceMock, varArgs[0]);340 PowerMock.verify(serviceMock, VarArgsConstructorDemo.class, varArgsConstructorDemoMock);341 }342 @Test343 public void testNewWithArrayVarArgs() throws Exception {344 ExpectNewDemo tested = new ExpectNewDemo();345 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);346 final byte[] byteArrayOne = new byte[]{ 42 };347 final byte[] byteArrayTwo = new byte[]{ 17 };348 PowerMock.expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo).andReturn(varArgsConstructorDemoMock);349 expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][]{ byteArrayOne });350 PowerMock.replay(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);351 byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);352 Assert.assertEquals(1, varArgs.length);353 Assert.assertSame(byteArrayOne, varArgs[0]);354 PowerMock.verify(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);355 }356 @Test357 public void testNewWithArrayVarArgsAndMatchers() throws Exception {358 ExpectNewDemo tested = new ExpectNewDemo();359 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);360 final byte[] byteArrayOne = new byte[]{ 42 };361 final byte[] byteArrayTwo = new byte[]{ 17 };362 PowerMock.expectNew(VarArgsConstructorDemo.class, aryEq(byteArrayOne), aryEq(byteArrayTwo)).andReturn(varArgsConstructorDemoMock);363 expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][]{ byteArrayOne });364 PowerMock.replay(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);365 byte[][] varArgs = tested.newVarArgsWithMatchers();366 Assert.assertEquals(1, varArgs.length);367 Assert.assertSame(byteArrayOne, varArgs[0]);368 PowerMock.verify(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);369 }370 @Test371 public void testNewWithArrayVarArgsWhenFirstArgumentIsNullAndSubseqentArgumentsAreNotNull() throws Exception {372 ExpectNewDemo tested = new ExpectNewDemo();373 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);374 final byte[] byteArrayOne = null;375 final byte[] byteArrayTwo = new byte[]{ 17 };376 PowerMock.expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo).andReturn(varArgsConstructorDemoMock);377 expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][]{ byteArrayTwo });378 PowerMock.replay(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);379 byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);380 Assert.assertEquals(1, varArgs.length);381 Assert.assertSame(byteArrayTwo, varArgs[0]);382 PowerMock.verify(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);383 }384 @Test385 public void testNewWithArrayVarArgsWhenFirstArgumentIsNotNullButSubseqentArgumentsAreNull() throws Exception {386 ExpectNewDemo tested = new ExpectNewDemo();387 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);388 final byte[] byteArrayOne = new byte[]{ 42 };389 final byte[] byteArrayTwo = null;390 PowerMock.expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo).andReturn(varArgsConstructorDemoMock);391 expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][]{ byteArrayOne });392 PowerMock.replay(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);393 byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);394 Assert.assertEquals(1, varArgs.length);395 Assert.assertSame(byteArrayOne, varArgs[0]);396 PowerMock.verify(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);397 }398 @Test399 public void testNewWithArrayVarArgsWhenFirstArgumentIsNullSecondArgumentIsNotNullAndThirdArgumentIsNull() throws Exception {400 ExpectNewDemo tested = new ExpectNewDemo();401 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);402 final byte[] byteArrayOne = null;403 final byte[] byteArrayTwo = new byte[]{ 42 };404 final byte[] byteArrayThree = null;405 PowerMock.expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo, byteArrayThree).andReturn(varArgsConstructorDemoMock);406 expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][]{ byteArrayTwo });407 PowerMock.replay(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);408 byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo, byteArrayThree);409 Assert.assertEquals(1, varArgs.length);410 Assert.assertSame(byteArrayTwo, varArgs[0]);411 PowerMock.verify(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);412 }413 @Test414 public void testNewWithArrayVarArgsWhenAllArgumentsAreNull() throws Exception {415 ExpectNewDemo tested = new ExpectNewDemo();416 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);417 final byte[] byteArrayOne = null;418 final byte[] byteArrayTwo = null;419 PowerMock.expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo).andReturn(varArgsConstructorDemoMock);420 expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][]{ byteArrayTwo });421 PowerMock.replay(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);422 byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);423 Assert.assertEquals(1, varArgs.length);424 Assert.assertSame(byteArrayTwo, varArgs[0]);425 PowerMock.verify(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);426 }427 @Test428 public void testNewWithWrongArgument() throws Exception {429 final int numberOfTimes = 2;430 final String expected = "used";431 ExpectNewDemo tested = new ExpectNewDemo();432 ExpectNewServiceUser expectNewServiceImplMock = PowerMock.createMock(ExpectNewServiceUser.class);433 Service serviceMock = PowerMock.createMock(Service.class);434 PowerMock.expectNew(ExpectNewServiceUser.class, serviceMock, numberOfTimes).andReturn(expectNewServiceImplMock);435 expect(expectNewServiceImplMock.useService()).andReturn(expected);436 PowerMock.replay(expectNewServiceImplMock, serviceMock, ExpectNewServiceUser.class);437 try {438 Assert.assertEquals(expected, tested.newWithWrongArguments(serviceMock, numberOfTimes));439 PowerMock.verify(expectNewServiceImplMock, serviceMock, ExpectNewServiceUser.class);440 Assert.fail("Should throw AssertionError!");441 } catch (AssertionError e) {442 Assert.assertEquals(("\n Unexpected constructor call samples.expectnew.ExpectNewServiceUser(EasyMock for interface samples.Service, 4 (int)):" + "\n samples.expectnew.ExpectNewServiceUser(EasyMock for interface samples.Service, 2 (int)): expected: 1, actual: 0"), e.getMessage());443 }444 }445 @Test446 public void testExpectNewButNoNewCallWasMade() throws Exception {447 ExpectNewDemo tested = new ExpectNewDemo();448 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);449 PowerMock.expectNew(MyClass.class).andReturn(myClassMock1).once();450 PowerMock.replay(myClassMock1, MyClass.class);451 try {452 tested.makeDate();453 PowerMock.verify(myClassMock1, MyClass.class);454 Assert.fail("Should throw AssertionError!");455 } catch (AssertionError e) {456 Assert.assertTrue(e.getMessage().contains(((MyClass.class.getName()) + "(): expected: 1, actual: 0")));457 }458 }459 @Test460 public void canDefineSeveralMockResultForNew() throws Exception {461 final Target expectedTarget = new Target(ExpectNewCases.UNKNOWN_TARGET_NAME, ExpectNewCases.UNKNOWN_TARGET_ID);462 PowerMock.expectNew(Target.class, ExpectNewCases.TARGET_NAME, ExpectNewCases.TARGET_ID).andThrow(new CreationException());463 PowerMock.expectNew(Target.class, "Unknown", (-1)).andReturn(expectedTarget);464 PowerMock.replay(Target.class);465 Target actualTarget = new ExpectNewDemo().createTarget(new ITarget() {466 @Override467 public int getId() {468 return ExpectNewCases.TARGET_ID;469 }470 @Override471 public String getName() {472 return ExpectNewCases.TARGET_NAME;473 }474 });475 assertThat(actualTarget).isEqualToComparingFieldByField(expectedTarget);476 }477}...
Target
Using AI Code Generation
1import samples.expectnew.Target2import samples.expectnew.Target.*3fun main() {4 val target = Target()5 target.method1()6 target.method2()7 target.method3()8 target.method4()9 target.method5()10 target.method6()11 target.method7()12 target.method8()13 target.method9()14 target.method10()15 target.method11()16 target.method12()17 target.method13()18 target.method14()19 target.method15()20 target.method16()21 target.method17()22 target.method18()23 target.method19()24 target.method20()25 target.method21()26 target.method22()27 target.method23()28 target.method24()29 target.method25()30 target.method26()31 target.method27()32 target.method28()33 target.method29()34 target.method30()35 target.method31()36 target.method32()37 target.method33()38 target.method34()39 target.method35()40 target.method36()41 target.method37()42 target.method38()43 target.method39()44 target.method40()45 target.method41()46 target.method42()47 target.method43()48 target.method44()49 target.method45()50 target.method46()51 target.method47()52 target.method48()53 target.method49()54 target.method50()55 target.method51()56 target.method52()57 target.method53()58 target.method54()59 target.method55()60 target.method56()61 target.method57()62 target.method58()63 target.method59()64 target.method60()65 target.method61()66 target.method62()67 target.method63()68 target.method64()69 target.method65()70 target.method66()71 target.method67()72 target.method68()73 target.method69()74 target.method70()75 target.method71()76 target.method72()77 target.method73()78 target.method74()79 target.method75()80 target.method76()81 target.method77()82 target.method78()83 target.method79()84 target.method80()85 target.method81()86 target.method82()87 target.method83()88 target.method84()89 target.method85()90 target.method86()91 target.method87()92 target.method88()93 target.method89()94 target.method90()95 target.method91()96 target.method92()97 target.method93()98 target.method94()99 target.method95()
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!!