Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Super keyword prevents mocking of method with a spy #487

Open
johanhaleby opened this issue Jul 25, 2015 · 2 comments
Open

Super keyword prevents mocking of method with a spy #487

johanhaleby opened this issue Jul 25, 2015 · 2 comments

Comments

@johanhaleby
Copy link
Collaborator

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 {

@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);
}
}

Original issue: http://code.google.com/p/powermock/issues/detail?id=467

@ercanpinar
Copy link

ercanpinar commented Oct 30, 2017

I have the same problem.
if I change "super.doSomething(id); => doSomething(id);" (without super) it is working.
I cannot change project resource.

Libraries:
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile "org.robolectric:robolectric:3.4.2"
testCompile "org.powermock:powermock-module-junit4:1.6.4"
testCompile "org.powermock:powermock-module-junit4-rule:1.6.4"
testCompile "org.powermock:powermock-api-mockito:1.6.4"
testCompile "org.powermock:powermock-classloading-xstream:1.6.4"

Thanks

@tjannrey
Copy link

tjannrey commented Oct 30, 2019

Hi, I've encountered the same problem.. Is this already solved? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants