Reputation: 145
I have developed one spring sample project with a controller and called a static method in it.
When I try to run the code, it leads me to an exception.
LoginController .java
@Controller
public class LoginController {@ResponseBody
@RequestMapping(value="/login")
public String login(){
System.out.println("in login method");
String s = SampleClass.method();
System.out.println("String value : "+s);
return "welcome to login page";
}}
SampleClass.java
public class SampleClass {
public static String method(){
System.out.println("in static method");
return "static method";
}}
LoginControllerTest.java
@PrepareForTest(SampleClass.class)
public class LoginControllerTest {
private MockMvc mockMvc;
@InjectMocks
private LoginController loginController;
@Before
public void setUp() {
System.out.println("in setup");
PowerMockito.mockStatic(SampleClass.class);
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(loginController).build();
System.out.println("setupdone");
}
@Test
public void loginTest() throws Exception{
System.out.println("in login test");
PowerMockito.when(SampleClass.method()).thenReturn("hello");
mockMvc.perform(get("/login"))
//.andDo(print())
.andExpect(status().isOk());
System.out.println("test completed");
}}
Error
org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because: 1. you stub either of: final/private/equals()/hashCode() methods. Those methods cannot be stubbed/verified. Mocking methods declared on non-public parent classes is not supported. 2. inside when() you don't call method on mock but on some other object.
at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:495) at com.junit.example.controller.LoginControllerTest.setUp(LoginControllerTest.java:57) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Can any one help me out with a solution?
Upvotes: 5
Views: 10181
Reputation: 3915
Based on the docs, you're not using this correctly: https://github.com/powermock/powermock/wiki/MockitoUsage
From the docs:
@PrepareForTest(Static.class)
(You're not doing this)PowerMockito.mockStatic(Static.class);
Mockito.when()
. You are using PowerMockito.when()
Upvotes: 4
Reputation: 29306
Why are you making your Test complicated?, when you can simply write it as:
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import com.arpit.controller.LoginController;
public class LoginControllerTest {
@InjectMocks
private LoginController loginController;
@Before
public void setupMock() {
MockitoAnnotations.initMocks(this);
}
@Test
public void loginTest() {
String val = loginController.login();
assertEquals("welcome to login page", val);
}
}
Output on Console:
in login method
in static method
String value : static method
Upvotes: -2