私はmockitoを使ってテストケースを書いています。以下はテストケースのコードです。メソッドがjunitでnullを返すときのmockito
@RunWith(SpringRunner.class)
public class LoginControllerTest {
private MockMvc mockMvc;
@InjectMocks
private LoginService loginService;
@Mock
private LoginController loginController;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
// Setup Spring test in standalone mode
mockMvc = MockMvcBuilders.standaloneSetup(loginController).build();
}
@Test
public final void test() throws Exception {
// Assign
when(loginService.test()).thenReturn("hello");
// act
mockMvc.perform(get("/hello"))
// Assertion
.andExpect(status().isOk())
.andExpect(content().string("Message from service: hello"));
verify(loginService).test();
}
@Test
public final void usernameInvalidAndPassword() throws Exception {
User userData = new User();
userData.setUserName("[email protected]");
userData.setPassword("Passw0rd");
User userDataNew = new User();
userDataNew.setUserName("[email protected]");
userDataNew.setPassword("Passw0rd");
JSONObject requestBody = new JSONObject();
requestBody.put("userName", "[email protected]");
requestBody.put("password", "Passw0rd");
JSONObject responseBody = new JSONObject();
responseBody.put("status_code", "200");
responseBody.put("message", "ok");
// Assign
when(loginService.saveUser(userData)).thenReturn(userDataNew);
// act
mockMvc.perform(get("/login")
.param("userName", "[email protected]")
.param("password", "Passw0rd"))
// Assertion
.andExpect(status().isOk()).andExpect(content().json(responseBody.toString())).andDo(print());
}
最初のテストケースでは正常に動作しますが、2番目のテストでは常にnullが返されます。誰でも助けてくれますか?事前に感謝