Best Powermock code snippet using samples.powermockito.junit4.privatemocking.PrivateInstanceMockingCases.expectationsWorkWhenSpyingOnPrivateMethods
Source:PrivateInstanceMockingCases.java
...32import static org.powermock.api.mockito.PowerMockito.verifyPrivate;33import static org.powermock.api.mockito.PowerMockito.when;34public class PrivateInstanceMockingCases {35 @Test36 public void expectationsWorkWhenSpyingOnPrivateMethods() throws Exception {37 PrivateMethodDemo tested = spy(new PrivateMethodDemo());38 assertEquals("Hello Temp, you are 50 old.", tested.sayYear("Temp", 50));39 when(tested, "doSayYear", 12, "test").thenReturn("another");40 assertEquals("Hello Johan, you are 29 old.", tested.sayYear("Johan", 29));41 assertEquals("another", tested.sayYear("test", 12));42 verifyPrivate(tested).invoke("doSayYear", 12, "test");43 }44 45 @Test46 public void expectationsWorkWithArgumentMatchersWhenSpyingOnPrivateMethods() throws Exception {47 PrivateMethodDemo tested = spy(new PrivateMethodDemo());48 assertEquals("Hello Temp, you are 50 old.", tested.sayYear("Temp", 50));49 when(tested, "doSayYear", Mockito.anyInt(), Mockito.anyString()).thenReturn("another");50 assertEquals("another", tested.sayYear("Johan", 29));...
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!!