Reputation: 31
Hello I have this PowerMockito test which throws anì UnfinishedStubbingException
@RunWith(PowerMockRunner.class)
@PrepareForTest(SuperHero.class)
public class SMSActionPresaInCaricoTest {
@Test
public void testExecute() {
PowerMockito.mockStatic(SuperHero.class);
when(SuperHero.findSuperHero(anyString())).thenReturn(new SuperHero ());
Action action = new Action();
action.execute("", "");
}
The class SuperHero has a static method findSuperHero that returns an instance of SuperHero reading the data from DB.
I think the error is due to the fact that thenReturn is trying to return an instance of the mocked class SuperHero.
Is there a way to use PowerMockito with this scenario? Or is it some refactoring necessary?
Upvotes: 0
Views: 768
Reputation: 64959
Sorry, cannot reproduce this one.
Here's my full test class, including all the imports:
package com.example;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.junit.Assert.assertNotNull;
@RunWith(PowerMockRunner.class)
@PrepareForTest(SuperHero.class)
public class SMSActionPresaInCaricoTest {
@Test
public void testExecute() {
PowerMockito.mockStatic(SuperHero.class);
when(SuperHero.findSuperHero(anyString())).thenReturn(new SuperHero());
assertNotNull(SuperHero.findSuperHero("Batman"));
}
}
When I ran this test, it passed.
I didn't have your SuperHero
class, so I used this one instead:
package com.example;
public class SuperHero {
public static SuperHero findSuperHero(String name) {
throw new RuntimeException("This method should have been mocked");
}
}
I'm using PowerMockito 1.6.2, in case that helps.
Upvotes: 1