You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using Java 6, JUnit 4.11, Mockito 1.9.5 and PowerMock 1.5.
Problem statement:
Having two classes where one extends the other, say:
public class Parent {
public void doSomething(String id) {
}
}
public class Child extends Parent {
public void doSomethingElse(String id) {
//do stuff
super.doSomething(id);
}
}
The methods are different and do not override. In my test of Child, I need to both mock a constructor and spy the Child class. For the spy, I need to do nothing when the method doSomething(...) is called. My test class looks something like this:
@RunWith(PowerMockRunner.class)
@PrepareForTest(Child.class) //In order to mock a constructor
public class ChildTest {
@InjectMocks
private Child child;
@Before
public void setUp() throws Exception {
initMocks(this);
}
@After
public void tearDown() throws Exception {
validate MockitoUsage ();
}
@Test
public void testDoSomethingElse() {
Child childSpy = PowerMocktio.spy(child);
PowerMockito.doNothing().when(childSpy).doSomething(anyString());
//Run the test:
childSpy.doSomethingElse("id");
}
}
My test fails as the doSomething(...) method in Parent is actually called. However, if I change the application code in Child and remove the super keyword, the spy mocks the method just fine and the test passes. To be specific, I have to change Child to look like this:
public class Child extends Parent {
public void doSomethingElse(String id) {
//do stuff
doSomething(id);
}
}
From UndeadHi...@gmail.com on October 16, 2013 00:54:29
I am using Java 6, JUnit 4.11, Mockito 1.9.5 and PowerMock 1.5.
Problem statement:
Having two classes where one extends the other, say:
public class Parent {
public void doSomething(String id) {
}
}
public class Child extends Parent {
public void doSomethingElse(String id) {
//do stuff
super.doSomething(id);
}
}
The methods are different and do not override. In my test of Child, I need to both mock a constructor and spy the Child class. For the spy, I need to do nothing when the method doSomething(...) is called. My test class looks something like this:
@RunWith(PowerMockRunner.class)
@PrepareForTest(Child.class) //In order to mock a constructor
public class ChildTest {
}
My test fails as the doSomething(...) method in Parent is actually called. However, if I change the application code in Child and remove the super keyword, the spy mocks the method just fine and the test passes. To be specific, I have to change Child to look like this:
public class Child extends Parent {
public void doSomethingElse(String id) {
//do stuff
doSomething(id);
}
}
Original issue: http://code.google.com/p/powermock/issues/detail?id=467
The text was updated successfully, but these errors were encountered: