How to use ExpectNewDemo class of samples.expectnew package

Best Powermock code snippet using samples.expectnew.ExpectNewDemo

Source:ExpectNewCases.java Github

copy

Full Screen

...9import org.powermock.reflect.Whitebox;10import org.powermock.reflect.exceptions.ConstructorNotFoundException;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}...

Full Screen

Full Screen

Source:ExpectNewDemoUsingThePrepareEverythingAnnotationTest.java Github

copy

Full Screen

...26import org.powermock.modules.junit4.PowerMockRunner;27import org.powermock.reflect.Whitebox;28import org.powermock.reflect.exceptions.ConstructorNotFoundException;29import samples.Service;30import samples.expectnew.ExpectNewDemo;31import samples.expectnew.ExpectNewServiceUser;32import samples.expectnew.VarArgsConstructorDemo;33import samples.newmocking.MyClass;34/**35 * Test class to demonstrate new instance mocking using expectNew(..) with the36 * {@link PrepareEverythingForTest} annotation and replayAll and verifyAll.37 */38@RunWith(PowerMockRunner.class)39@PrepareEverythingForTest40public class ExpectNewDemoUsingThePrepareEverythingAnnotationTest {41 @Test42 public void testNewWithCheckedException() throws Exception {43 ExpectNewDemo tested = new ExpectNewDemo();44 final String expectedFailMessage = "testing checked exception";45 expectNew(MyClass.class).andThrow(new IOException(expectedFailMessage));46 replayAll();47 try {48 tested.throwExceptionAndWrapInRunTimeWhenInvoction();49 Assert.fail("Should throw a checked Exception!");50 } catch (RuntimeException e) {51 Assert.assertTrue(((e.getCause()) instanceof IOException));52 Assert.assertEquals(expectedFailMessage, e.getMessage());53 }54 verifyAll();55 }56 @PrepareEverythingForTest57 @Test58 public void testGetMessage() throws Exception {59 ExpectNewDemo tested = new ExpectNewDemo();60 MyClass myClassMock = PowerMock.createMock(MyClass.class);61 expectNew(MyClass.class).andReturn(myClassMock);62 String expected = "Hello altered World";63 expect(myClassMock.getMessage()).andReturn("Hello altered World");64 replayAll();65 String actual = tested.getMessage();66 verifyAll();67 Assert.assertEquals("Expected and actual did not match", expected, actual);68 }69 @Test70 public void testGetMessageWithArgument() throws Exception {71 ExpectNewDemo tested = new ExpectNewDemo();72 MyClass myClassMock = PowerMock.createMock(MyClass.class);73 expectNew(MyClass.class).andReturn(myClassMock);74 String expected = "Hello altered World";75 expect(myClassMock.getMessage("test")).andReturn("Hello altered World");76 replayAll();77 String actual = tested.getMessageWithArgument();78 verifyAll();79 Assert.assertEquals("Expected and actual did not match", expected, actual);80 }81 @Test82 public void testInvokeVoidMethod() throws Exception {83 ExpectNewDemo tested = new ExpectNewDemo();84 MyClass myClassMock = PowerMock.createMock(MyClass.class);85 expectNew(MyClass.class).andReturn(myClassMock);86 myClassMock.voidMethod();87 expectLastCall().times(1);88 replayAll();89 tested.invokeVoidMethod();90 verifyAll();91 }92 @Test93 public void testNewWithRuntimeException() throws Exception {94 ExpectNewDemo tested = new ExpectNewDemo();95 final String expectedFailMessage = "testing";96 expectNew(MyClass.class).andThrow(new RuntimeException(expectedFailMessage));97 replayAll();98 try {99 tested.throwExceptionWhenInvoction();100 Assert.fail("Should throw RuntimeException!");101 } catch (RuntimeException e) {102 Assert.assertEquals(expectedFailMessage, e.getMessage());103 }104 verifyAll();105 }106 @Test107 public void testPreviousProblemsWithByteCodeManipulation() throws Exception {108 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);109 expect(myClassMock1.getMessage()).andReturn("Hello");110 expect(myClassMock1.getMessage()).andReturn("World");111 replayAll();112 Assert.assertEquals("Hello", myClassMock1.getMessage());113 Assert.assertEquals("World", myClassMock1.getMessage());114 verifyAll();115 }116 @Test117 public void testMultipleNew() throws Exception {118 ExpectNewDemo tested = new ExpectNewDemo();119 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);120 MyClass myClassMock2 = PowerMock.createMock(MyClass.class);121 expectNew(MyClass.class).andReturn(myClassMock1);122 expectNew(MyClass.class).andReturn(myClassMock2);123 expect(myClassMock1.getMessage()).andReturn("Hello ");124 expect(myClassMock2.getMessage()).andReturn("World");125 replayAll();126 final String actual = tested.multipleNew();127 verifyAll();128 Assert.assertEquals("Hello World", actual);129 }130 @Test131 public void testSimpleMultipleNew() throws Exception {132 ExpectNewDemo tested = new ExpectNewDemo();133 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);134 expectNew(MyClass.class).andReturn(myClassMock1).times(3);135 replayAll();136 tested.simpleMultipleNew();137 verifyAll();138 }139 @Test140 public void testSimpleMultipleNew_tooManyTimesExpected() throws Exception {141 ExpectNewDemo tested = new ExpectNewDemo();142 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);143 expectNew(MyClass.class).andReturn(myClassMock1).times(4);144 replayAll();145 tested.simpleMultipleNew();146 try {147 verifyAll();148 Assert.fail("Should throw AssertionError.");149 } catch (AssertionError e) {150 Assert.assertEquals(("\n Expectation failure on verify:" + "\n samples.newmocking.MyClass(): expected: 4, actual: 3"), e.getMessage());151 }152 }153 @Test154 public void testSimpleMultipleNew_tooFewTimesExpected() throws Exception {155 ExpectNewDemo tested = new ExpectNewDemo();156 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);157 expectNew(MyClass.class).andReturn(myClassMock1).times(2);158 replayAll();159 try {160 tested.simpleMultipleNew();161 Assert.fail("Should throw AssertionError.");162 } catch (AssertionError e) {163 Assert.assertEquals(("\n Unexpected constructor call samples.newmocking.MyClass():" + "\n samples.newmocking.MyClass(): expected: 2, actual: 3"), e.getMessage());164 }165 }166 /**167 * Verifies that the issue168 * http://code.google.com/p/powermock/issues/detail?id=10 is solved.169 */170 @Test171 public void testSimpleMultipleNewPrivate_tooFewTimesExpected() throws Exception {172 ExpectNewDemo tested = new ExpectNewDemo();173 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);174 expectNew(MyClass.class).andReturn(myClassMock1).times(2);175 replayAll();176 try {177 Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");178 Assert.fail("Should throw AssertionError.");179 } catch (AssertionError e) {180 Assert.assertEquals(("\n Unexpected constructor call samples.newmocking.MyClass():" + "\n samples.newmocking.MyClass(): expected: 2, actual: 3"), e.getMessage());181 }182 }183 @Test184 public void testSimpleMultipleNewPrivate_ok() throws Exception {185 ExpectNewDemo tested = new ExpectNewDemo();186 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);187 expectNew(MyClass.class).andReturn(myClassMock1).times(3);188 replayAll();189 Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");190 }191 @Test192 public void testSimpleSingleNew_withOnce() throws Exception {193 ExpectNewDemo tested = new ExpectNewDemo();194 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);195 expectNew(MyClass.class).andReturn(myClassMock1).once();196 replayAll();197 tested.simpleSingleNew();198 verifyAll();199 }200 @Test201 public void testSimpleSingleNew_withAtLeastOnce() throws Exception {202 ExpectNewDemo tested = new ExpectNewDemo();203 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);204 expectNew(MyClass.class).andReturn(myClassMock1).atLeastOnce();205 replayAll();206 tested.simpleSingleNew();207 verifyAll();208 }209 @Test210 public void testSimpleMultipleNew_withAtLeastOnce() throws Exception {211 ExpectNewDemo tested = new ExpectNewDemo();212 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);213 expectNew(MyClass.class).andReturn(myClassMock1).atLeastOnce();214 replayAll();215 tested.simpleMultipleNew();216 verifyAll();217 }218 @Test219 public void testSimpleMultipleNew_withRange_lowerBoundLessThan0() throws Exception {220 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);221 try {222 expectNew(MyClass.class).andReturn(myClassMock1).times((-20), 2);223 Assert.fail("Should throw IllegalArgumentException.");224 } catch (IllegalArgumentException e) {225 Assert.assertEquals("minimum must be >= 0", e.getMessage());226 }227 }228 @Test229 public void testSimpleMultipleNew_withRange_upperBoundLessThan0() throws Exception {230 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);231 try {232 expectNew(MyClass.class).andReturn(myClassMock1).times((-1), (-2));233 Assert.fail("Should throw IllegalArgumentException.");234 } catch (IllegalArgumentException e) {235 Assert.assertTrue(e.getMessage().contains("<="));236 }237 }238 @Test239 public void testSimpleMultipleNew_withRange_upperBoundLessThanLowerBound() throws Exception {240 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);241 try {242 expectNew(MyClass.class).andReturn(myClassMock1).times(10, 2);243 Assert.fail("Should throw IllegalArgumentException.");244 } catch (IllegalArgumentException e) {245 Assert.assertTrue(e.getMessage().contains("<="));246 }247 }248 @Test249 public void testSimpleMultipleNew_withRange_OK() throws Exception {250 ExpectNewDemo tested = new ExpectNewDemo();251 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);252 expectNew(MyClass.class).andReturn(myClassMock1).times(1, 5);253 replayAll();254 tested.simpleMultipleNew();255 verifyAll();256 }257 @Test258 public void testSimpleMultipleNew_anyTimes() throws Exception {259 ExpectNewDemo tested = new ExpectNewDemo();260 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);261 expectNew(MyClass.class).andReturn(myClassMock1).anyTimes();262 replayAll();263 tested.simpleMultipleNew();264 verifyAll();265 }266 @Test267 public void testSimpleMultipleNew_withRange_notWithinRange() throws Exception {268 ExpectNewDemo tested = new ExpectNewDemo();269 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);270 expectNew(MyClass.class).andReturn(myClassMock1).times(5, 7);271 replayAll();272 tested.simpleMultipleNew();273 try {274 verifyAll();275 Assert.fail("Should throw AssertionError.");276 } catch (AssertionError e) {277 Assert.assertEquals(("\n Expectation failure on verify:" + "\n samples.newmocking.MyClass(): expected: between 5 and 7, actual: 3"), e.getMessage());278 }279 }280 @Test281 public void testAlternativeFlow() throws Exception {282 ExpectNewDemo tested = new ExpectNewDemo();283 expectNew(DataInputStream.class, new Object[]{ null }).andThrow(new RuntimeException("error"));284 replayAll();285 InputStream stream = tested.alternativePath();286 verifyAll();287 Assert.assertNotNull("The returned inputstream should not be null.", stream);288 Assert.assertTrue("The returned inputstream should be an instance of ByteArrayInputStream.", (stream instanceof ByteArrayInputStream));289 }290 @Test291 public void testSimpleMultipleNewPrivate_tooManyTimesExpected() throws Exception {292 ExpectNewDemo tested = new ExpectNewDemo();293 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);294 expectNew(MyClass.class).andReturn(myClassMock1).times(4);295 replayAll();296 try {297 Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");298 verifyAll();299 Assert.fail("Should throw an exception!.");300 } catch (AssertionError e) {301 Assert.assertEquals(("\n Expectation failure on verify:" + "\n samples.newmocking.MyClass(): expected: 4, actual: 3"), e.getMessage());302 }303 }304 @Test305 public void testNewWithArguments() throws Exception {306 final int numberOfTimes = 2;307 final String expected = "used";308 ExpectNewDemo tested = new ExpectNewDemo();309 ExpectNewServiceUser expectNewServiceImplMock = PowerMock.createMock(ExpectNewServiceUser.class);310 Service serviceMock = PowerMock.createMock(Service.class);311 expectNew(ExpectNewServiceUser.class, serviceMock, numberOfTimes).andReturn(expectNewServiceImplMock);312 expect(expectNewServiceImplMock.useService()).andReturn(expected);313 replayAll();314 Assert.assertEquals(expected, tested.newWithArguments(serviceMock, numberOfTimes));315 verifyAll();316 }317 @Test318 public void testNewWithVarArgs() throws Exception {319 final String firstString = "hello";320 final String secondString = "world";321 ExpectNewDemo tested = new ExpectNewDemo();322 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);323 expectNew(VarArgsConstructorDemo.class, firstString, secondString).andReturn(varArgsConstructorDemoMock);324 expect(varArgsConstructorDemoMock.getAllMessages()).andReturn(new String[]{ firstString, secondString });325 replayAll();326 String[] varArgs = tested.newVarArgs(firstString, secondString);327 Assert.assertEquals(2, varArgs.length);328 Assert.assertEquals(firstString, varArgs[0]);329 Assert.assertEquals(secondString, varArgs[1]);330 verifyAll();331 }332 @Test333 public void testNewWhenTheExpectedConstructorIsNotFound() throws Exception {334 final Object object = new Object();335 try {336 expectNew(VarArgsConstructorDemo.class, object);337 Assert.fail("Should throw ConstructorNotFoundException!");338 } catch (ConstructorNotFoundException e) {339 Assert.assertEquals((((("No constructor found in class '" + (VarArgsConstructorDemo.class.getName())) + "' with parameter types: [ ") + (object.getClass().getName())) + " ]."), e.getMessage());340 }341 }342 @Test343 public void testNewWithVarArgsConstructorWhenOneArgumentIsOfASubType() throws Exception {344 ExpectNewDemo tested = new ExpectNewDemo();345 Service serviceMock = PowerMock.createMock(Service.class);346 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);347 final Service serviceSubTypeInstance = new Service() {348 @Override349 public String getServiceMessage() {350 return "message";351 }352 };353 expectNew(VarArgsConstructorDemo.class, serviceSubTypeInstance, serviceMock).andReturn(varArgsConstructorDemoMock);354 expect(varArgsConstructorDemoMock.getAllServices()).andReturn(new Service[]{ serviceMock });355 replayAll();356 Service[] varArgs = tested.newVarArgs(serviceSubTypeInstance, serviceMock);357 Assert.assertEquals(1, varArgs.length);358 Assert.assertSame(serviceMock, varArgs[0]);359 verifyAll();360 }361 @Test362 public void testNewWithArrayVarArgs() throws Exception {363 ExpectNewDemo tested = new ExpectNewDemo();364 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);365 final byte[] byteArrayOne = new byte[]{ 42 };366 final byte[] byteArrayTwo = new byte[]{ 17 };367 expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo).andReturn(varArgsConstructorDemoMock);368 expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][]{ byteArrayOne });369 replayAll();370 byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);371 Assert.assertEquals(1, varArgs.length);372 Assert.assertSame(byteArrayOne, varArgs[0]);373 verifyAll();374 }375 @Test376 public void testNewWithArrayVarArgsAndMatchers() throws Exception {377 ExpectNewDemo tested = new ExpectNewDemo();378 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);379 final byte[] byteArrayOne = new byte[]{ 42 };380 final byte[] byteArrayTwo = new byte[]{ 17 };381 expectNew(VarArgsConstructorDemo.class, aryEq(byteArrayOne), aryEq(byteArrayTwo)).andReturn(varArgsConstructorDemoMock);382 expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][]{ byteArrayOne });383 replayAll();384 byte[][] varArgs = tested.newVarArgsWithMatchers();385 Assert.assertEquals(1, varArgs.length);386 Assert.assertSame(byteArrayOne, varArgs[0]);387 verifyAll();388 }389 @Test390 public void testNewWithArrayVarArgsWhenFirstArgumentIsNullAndSubseqentArgumentsAreNotNull() throws Exception {391 ExpectNewDemo tested = new ExpectNewDemo();392 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);393 final byte[] byteArrayOne = null;394 final byte[] byteArrayTwo = new byte[]{ 17 };395 expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo).andReturn(varArgsConstructorDemoMock);396 expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][]{ byteArrayTwo });397 replayAll();398 byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);399 Assert.assertEquals(1, varArgs.length);400 Assert.assertSame(byteArrayTwo, varArgs[0]);401 verifyAll();402 }403 @Test404 public void testNewWithArrayVarArgsWhenFirstArgumentIsNotNullButSubseqentArgumentsAreNull() throws Exception {405 ExpectNewDemo tested = new ExpectNewDemo();406 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);407 final byte[] byteArrayOne = new byte[]{ 42 };408 final byte[] byteArrayTwo = null;409 expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo).andReturn(varArgsConstructorDemoMock);410 expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][]{ byteArrayOne });411 replayAll();412 byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);413 Assert.assertEquals(1, varArgs.length);414 Assert.assertSame(byteArrayOne, varArgs[0]);415 verifyAll();416 }417 @Test418 public void testNewWithArrayVarArgsWhenFirstArgumentIsNullSecondArgumentIsNotNullAndThirdArgumentIsNull() throws Exception {419 ExpectNewDemo tested = new ExpectNewDemo();420 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);421 final byte[] byteArrayOne = null;422 final byte[] byteArrayTwo = new byte[]{ 42 };423 final byte[] byteArrayThree = null;424 expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo, byteArrayThree).andReturn(varArgsConstructorDemoMock);425 expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][]{ byteArrayTwo });426 replayAll();427 byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo, byteArrayThree);428 Assert.assertEquals(1, varArgs.length);429 Assert.assertSame(byteArrayTwo, varArgs[0]);430 verifyAll();431 }432 @Test433 public void testNewWithArrayVarArgsWhenAllArgumentsAreNull() throws Exception {434 ExpectNewDemo tested = new ExpectNewDemo();435 VarArgsConstructorDemo varArgsConstructorDemoMock = PowerMock.createMock(VarArgsConstructorDemo.class);436 final byte[] byteArrayOne = null;437 final byte[] byteArrayTwo = null;438 expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo).andReturn(varArgsConstructorDemoMock);439 expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][]{ byteArrayTwo });440 replayAll();441 byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);442 Assert.assertEquals(1, varArgs.length);443 Assert.assertSame(byteArrayTwo, varArgs[0]);444 verifyAll();445 }446 @Test447 public void testNewWithWrongArgument() throws Exception {448 final int numberOfTimes = 2;449 final String expected = "used";450 ExpectNewDemo tested = new ExpectNewDemo();451 ExpectNewServiceUser expectNewServiceImplMock = PowerMock.createMock(ExpectNewServiceUser.class);452 Service serviceMock = PowerMock.createMock(Service.class);453 expectNew(ExpectNewServiceUser.class, serviceMock, numberOfTimes).andReturn(expectNewServiceImplMock);454 expect(expectNewServiceImplMock.useService()).andReturn(expected);455 replayAll();456 try {457 Assert.assertEquals(expected, tested.newWithWrongArguments(serviceMock, numberOfTimes));458 verifyAll();459 Assert.fail("Should throw AssertionError!");460 } catch (AssertionError e) {461 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());462 }463 }464 @Test465 public void testExpectNewButNoNewCallWasMade() throws Exception {466 ExpectNewDemo tested = new ExpectNewDemo();467 MyClass myClassMock1 = PowerMock.createMock(MyClass.class);468 expectNew(MyClass.class).andReturn(myClassMock1).once();469 replayAll();470 try {471 tested.makeDate();472 verifyAll();473 Assert.fail("Should throw AssertionError!");474 } catch (AssertionError e) {475 Assert.assertTrue(e.getMessage().contains(((MyClass.class.getName()) + "(): expected: 1, actual: 0")));476 }477 }478}...

Full Screen

Full Screen

ExpectNewDemo

Using AI Code Generation

copy

Full Screen

1package samples.expectnew;2import org.easymock.EasyMock;3import org.easymock.EasyMockSupport;4import org.junit.Test;5public class ExpectNewDemoTest extends EasyMockSupport {6 public void test() {7 ExpectNewDemo mock = createMock(ExpectNewDemo.class);8 expectNew(ExpectNewDemo.class).andReturn(mock);9 replayAll();10 ExpectNewDemo demo = new ExpectNewDemo();11 verifyAll();12 }13}14package samples.expectnew;15public class ExpectNewDemo {16 public ExpectNewDemo() {17 System.out.println("ExpectNewDemo Constructor");18 }19}

Full Screen

Full Screen

ExpectNewDemo

Using AI Code Generation

copy

Full Screen

1package samples.expectnew;2import org.easymock.EasyMock;3import org.easymock.IMocksControl;4public class ExpectNewDemo {5 public static void main(String[] args) {6 IMocksControl control = EasyMock.createControl();7 ExpectNewDemo obj = control.createMock(ExpectNewDemo.class);8 obj.method1();9 EasyMock.expectNew(ExpectNewDemo.class).andReturn(obj);10 control.replay();11 ExpectNewDemo obj1 = new ExpectNewDemo();12 control.verify();13 }14 public void method1() {15 System.out.println("method1");16 }17}18package samples.expectnew;19import org.easymock.EasyMock;20import org.easymock.IMocksControl;21public class ExpectNewDemo {22 public static void main(String[] args) {23 IMocksControl control = EasyMock.createControl();24 ExpectNewDemo obj = control.createMock(ExpectNewDemo.class);25 obj.method1();26 EasyMock.expectNew(ExpectNewDemo.class).andReturn(obj);27 control.replay();28 ExpectNewDemo obj1 = new ExpectNewDemo();29 control.verify();30 }31 public void method1() {32 System.out.println("method1");33 }34}35package samples.expectnew;36import org.easymock.EasyMock;37import org.easymock.IMocksControl;38public class ExpectNewDemo {39 public static void main(String[] args) {40 IMocksControl control = EasyMock.createControl();41 ExpectNewDemo obj = control.createMock(ExpectNewDemo.class);42 obj.method1();43 EasyMock.expectNew(ExpectNewDemo.class).andReturn(obj);44 control.replay();45 ExpectNewDemo obj1 = new ExpectNewDemo();46 control.verify();47 }48 public void method1() {49 System.out.println("method1");50 }51}52package samples.expectnew;53import org.easymock.EasyMock;54import org.easymock.IMocksControl;55public class ExpectNewDemo {56 public static void main(String[] args) {57 IMocksControl control = EasyMock.createControl();

Full Screen

Full Screen

ExpectNewDemo

Using AI Code Generation

copy

Full Screen

1package com.expectnew;2import org.easymock.EasyMock;3import org.easymock.EasyMockSupport;4import org.junit.Test;5public class ExpectNewDemoTest extends EasyMockSupport {6 public void testExpectNew() {7 NewClassDemo mock = createMock(NewClassDemo.class);8 EasyMock.expectNew(NewClassDemo.class).andReturn(mock);9 replayAll();10 ExpectNewDemo expectNewDemo = new ExpectNewDemo();11 expectNewDemo.test();12 verifyAll();13 }14}15package com.expectnew;16public class NewClassDemo {17 public NewClassDemo() {18 System.out.println("NewClassDemo constructor called");19 }20}21package com.expectnew;22public class ExpectNewDemo {23 public void test() {24 NewClassDemo newClassDemo = new NewClassDemo();25 }26}27Related posts: EasyMock: How to use createNiceMock() method? EasyMock: How to use createMockBuilder() method? EasyMock: How to use createNiceControl() method? EasyMock: How to use createControl() method? EasyMock: How to use createMock() method? EasyMock: How to use createMockBuilder() method? EasyMock: How to use createControl() method? EasyMock: How to use createNiceControl() method? EasyMock: How to use createMock() method? EasyMock: How to use createNiceMock() method? EasyMock: How to use createMockBuilder() method? EasyMock: How to use createControl() method? EasyMock: How to use createNiceControl() method? EasyMock: How to use createMock() method? EasyMock: How to use createNiceMock() method? EasyMock: How to use createMockBuilder() method? EasyMock: How to use createControl() method? EasyMock: How to use createNiceControl() method? EasyMock: How to use createMock() method? EasyMock: How to use createNiceMock() method? EasyMock: How to use createMockBuilder

Full Screen

Full Screen

ExpectNewDemo

Using AI Code Generation

copy

Full Screen

1package samples.expectnew;2import org.easymock.*;3import org.easymock.classextension.*;4import org.junit.*;5import static org.junit.Assert.*;6import static org.easymock.EasyMock.*;7public class ExpectNewDemoTest {8 public void testExpectNew() {9 MocksControl control = MocksControl.createControl();10 ExpectNewDemo mock = control.createMock(ExpectNewDemo.class);11 expectNew(ExpectNewDemo.class, "Hello").andReturn(mock);12 control.replay();13 ExpectNewDemo demo = new ExpectNewDemo("Hello");14 assertSame(mock, demo);15 control.verify();16 }17}18The method createMock() is used to create a mock object of the class ExpectNewDemo. The method expectNew() is used to specify the class whose constructor is to be called and the arguments to be passed to the constructor. The method expectNew() returns an instance of the class ExpectNewDemo which is then passed to the method

Full Screen

Full Screen

ExpectNewDemo

Using AI Code Generation

copy

Full Screen

1import samples.expectnew.ExpectNewDemo;2import samples.expectnew.ExpectNewDemo;3public class ExpectNewDemoTest {4 public static void main(String[] args) {5 ExpectNewDemo expectNewDemo = new ExpectNewDemo();6 expectNewDemo.doSomething();7 }8}9java -javaagent:mockito-core-1.9.5.jar -cp .;mockito-core-1.9.5.jar ExpectNewDemoTest10 at ExpectNewDemo.doSomething(ExpectNewDemo.java:10)11 at ExpectNewDemoTest.main(ExpectNewDemoTest.java:8)12import samples.expectnew.ExpectNewDemo;13import samples.expectnew.ExpectNewDemo;14import org.mockito.Mock;15import org.mockito.MockitoAnnotations;16import org.mockito.internal.util.reflection.Whitebox;17import org.mockito.junit.MockitoJUnitRunner;18import org.junit.runner.RunWith;19import org.junit.Before;20import org.junit.Test;21import org.mockito.InjectMocks;22import org.mockito.Mock;23import org.mockito.MockitoAnnotations;24import org.mockito.internal.util.reflection.Whitebox;25import org.mockito.junit.MockitoJUnitRunner;26import org.junit.runner.RunWith;27import org.junit.Before;28import org.junit.Test;29import org.mockito.InjectMocks;30import org.mockito.Mock;31import org.mockito.MockitoAnnotations;32import org.mockito.internal.util.reflection.Whitebox;33import org.mockito.junit.MockitoJUnitRunner;34import org.junit.runner.RunWith;35import org.junit.Before;36import org.junit.Test;37import org.mockito.InjectMocks;38import org.mockito.Mock;39import org.mockito.MockitoAnnotations;40import org.mockito.internal.util.reflection.Whitebox;41import org.mockito.junit.MockitoJUnitRunner;42import org.junit.runner.RunWith;43import org.junit.Before;44import org.junit.Test;45import org.mockito.InjectMocks;46import org.mockito.Mock;47import org.mockito.MockitoAnnotations;48import org.mockito.internal.util.reflection.Whitebox;49import org.mockito.junit.MockitoJUnitRunner;50import org.junit.runner.RunWith;51import org.junit.Before;52import org.junit.Test;53import org.mockito.InjectMocks;54import org.mockito.Mock;55import org.mockito.MockitoAnnotations;56import org.mockito.internal.util.reflection.Whitebox;57import org.mockito.junit

Full Screen

Full Screen

ExpectNewDemo

Using AI Code Generation

copy

Full Screen

1package samples.expectnew;2import org.easymock.EasyMock;3import org.easymock.EasyMockSupport;4public class ExpectNewDemoTest extends EasyMockSupport {5 public void test() {6 Object object = createMock(Object.class);7 expectNew(Object.class).andReturn(object);8 replayAll();9 Object object2 = new Object();10 verifyAll();11 }12}131. Example of EasyMock expectNew() method of EasyMockSupport class. 2. Example of EasyMock expectNew() method of EasyMockSupport class. 3. Example of EasyMock expectNew() method of EasyMockSupport class. 4. Example of EasyMock expectNew() method of EasyMockSupport class. 5. Example of EasyMock expectNew() method of EasyMockSupport class. 6. Example of EasyMock expectNew() method of EasyMockSupport class. 7. Example of EasyMock expectNew() method of EasyMockSupport class. 8. Example of EasyMock expectNew() method of EasyMockSupport class. 9. Example of EasyMock expectNew() method of EasyMockSupport class. 10. Example of EasyMock expectNew() method of EasyMockSupport class. 11. Example of EasyMock expectNew() method of EasyMockSupport class. 12. Example of EasyMock expectNew() method of EasyMockSupport class. 13. Example of EasyMock expectNew() method of EasyMockSupport class. 14. Example of EasyMock expectNew() method of EasyMockSupport class. 15. Example of EasyMock expectNew() method of EasyMockSupport class. 16. Example of EasyMock expectNew() method of EasyMockSupport class. 17. Example of EasyMock expectNew() method of EasyMockSupport class. 18. Example of EasyMock expectNew() method of EasyMockSupport class. 19. Example of EasyMock expectNew() method of EasyMockSupport class. 20. Example of EasyMock expectNew() method of EasyMockSupport class. 21. Example of EasyMock expectNew() method of EasyMockSupport class. 22. Example of EasyMock expectNew() method of EasyMockSupport class. 23. Example of EasyMock expectNew() method of

Full Screen

Full Screen

ExpectNewDemo

Using AI Code Generation

copy

Full Screen

1import samples.expectnew.ExpectNewDemo;2import java.util.Scanner;3public class ExpectNewDemoTest {4 public static void main(String args[]) {5 Scanner sc = new Scanner(System.in);6 System.out.println("Enter the value of a: ");7 int a = sc.nextInt();8 System.out.println("Enter the value of b: ");9 int b = sc.nextInt();10 System.out.println("Enter the value of c: ");11 int c = sc.nextInt();12 ExpectNewDemo expectNewDemo = new ExpectNewDemo();13 int result = expectNewDemo.add(a, b, c);14 System.out.println("Result: " + result);15 }16}

Full Screen

Full Screen

ExpectNewDemo

Using AI Code Generation

copy

Full Screen

1import samples.expectnew.ExpectNewDemo;2public class ExpectNewDemoTest {3 public static void main(String args[]){4 ExpectNewDemo obj = new ExpectNewDemo();5 System.out.println(obj.toString());6 }7}8import org.junit.Test;9import static org.junit.Assert.*;10import org.powermock.core.classloader.annotations.PrepareForTest;11import org.powermock.modules.junit4.PowerMockRunner;12import org.powermock.api.mockito.PowerMockito;13import static org.mockito.Mockito.*;14import org.junit.runner.RunWith;15import samples.expectnew.ExpectNewDemo;16@RunWith(PowerMockRunner.class)17@PrepareForTest(ExpectNewDemo.class)18public class ExpectNewDemoTest {19 public void testExpectNewDemo() throws Exception{20 ExpectNewDemo obj = PowerMockito.mock(ExpectNewDemo.class);21 whenNew(ExpectNewDemo.class).withNoArguments().thenReturn(obj);22 System.out.println(obj.toString());23 }24}25-> at ExpectNewDemoTest.testExpectNewDemo(ExpectNewDemoTest.java:19)26 when(mock.isOk()).thenReturn(true);27 when(mock.isOk()).thenThrow(exception);28 doThrow(exception).when(mock).someVoidMethod();29-> at ExpectNewDemoTest.testExpectNewDemo(ExpectNewDemoTest.java:19)

Full Screen

Full Screen

ExpectNewDemo

Using AI Code Generation

copy

Full Screen

1package samples.expectnew;2import java.io.IOException;3public class ExpectNewDemo {4 public String getOutput() throws IOException {5 return "Hello World!";6 }7}8package samples.expectnew;9import java.io.IOException;10import org.junit.Test;11import static org.junit.Assert.*;12import org.junit.runner.RunWith;13import org.powermock.api.easymock.annotation.Mock;14import org.powermock.core.classloader.annotations.PrepareForTest;15import org.powermock.modules.junit4.PowerMockRunner;16@RunWith(PowerMockRunner.class)17@PrepareForTest(ExpectNewDemo.class)18public class ExpectNewDemoTest {19 private ExpectNewDemo expectNewDemo;20 public void testGetOutput() throws IOException {21 String output = expectNewDemo.getOutput();22 assertEquals("Hello World!", output);23 }24}25 at org.powermock.api.easymock.internal.expectation.ConstructorCall.callConstructor(ConstructorCall.java:54)26 at org.powermock.api.easymock.internal.expectation.ConstructorCall.andReturn(ConstructorCall.java:65)27 at org.powermock.api.easymock.internal.expectation.ConstructorCall.andReturn(ConstructorCall.java:26)28 at samples.expectnew.ExpectNewDemoTest.testGetOutput(ExpectNewDemoTest.java:26)29 at java.net.URLClassLoader$1.run(URLClassLoader.java:372)30 at java.net.URLClassLoader$1.run(URLClassLoader.java:361)31 at java.security.AccessController.doPrivileged(Native Method)32 at java.net.URLClassLoader.findClass(URLClassLoader.java:360)33 at java.lang.ClassLoader.loadClass(ClassLoader.java:424)34 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)35 at java.lang.ClassLoader.loadClass(ClassLoader.java:357)36 at org.powermock.api.easymock.internal.expectation.ConstructorCall.callConstructor(ConstructorCall.java:52)37 at org.powermock.api.easymock.internal.expectation.ConstructorCall.callConstructor(ConstructorCall.java:54)

Full Screen

Full Screen

ExpectNewDemo

Using AI Code Generation

copy

Full Screen

1import samples.expectnew.*;2import java.util.*;3import java.io.*;4{5 public static void main(String[] args)6 {7 ExpectNewDemo obj = new ExpectNewDemo();8 obj.testMethod();9 }10 public void testMethod()11 {12 ArrayList spy = spy(new ArrayList());13 spy.add("Hello World");14 verify(spy).add("Hello World");15 }16}17import samples.expectnew.*;18import java.util.*;19import java.io.*;20{21 public static void main(String[] args)22 {23 ExpectNewDemo obj = new ExpectNewDemo();24 obj.testMethod();25 }26 public void testMethod()27 {28 ArrayList spy = spy(new ArrayList());29 spy.add("Hello World");30 verify(spy).add("Hello World");31 }32}33import samples.expectnew.*;34import java.util.*;35import java.io.*;36{37 public static void main(String[] args)38 {39 ExpectNewDemo obj = new ExpectNewDemo();40 obj.testMethod();41 }42 public void testMethod()43 {44 ArrayList spy = spy(new ArrayList());45 spy.add("Hello World");46 verify(spy).add("Hello World");47 }48}49import samples.expectnew.*;50import java.util.*;51import java.io.*;52{53 public static void main(String[] args)

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful