Mockito测试返回的状态不佳

时间:2017-08-17 23:40:37

标签: java spring spring-mvc spring-boot mockito

我想从控制器

测试一个示例方法
@GetMapping("/checkUsernameAtRegistering")
public HttpEntity<Boolean> checkUsernameAtRegistering(@RequestParam String username) {
            return ResponseEntity.ok().body(!userService.existsByUsername(username));
}

总是返回状态OK。

我在Mockito中为此创建了一个测试

public class CheckUserDataRestControllerTest {

@Mock
private UserService userService;

@InjectMocks
private CheckUserDataRestController checkUserDataRestController;

private MockMvc mockMvc;

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);

    mockMvc = MockMvcBuilders.standaloneSetup(checkUserDataRestController).build();
}

@Test
public void testCheckUsernameAtRegistering() throws Exception {
    mockMvc
            .perform(get("/checkUsernameAtRegistering")
            .param("username", "jonki97"))
            .andExpect(status().isOk());
}
}

我希望状态好。我知道这没有意义,但我只是想让测试正确完成。但是,成功完成请求后丢弃

java.lang.AssertionError: Status 
Expected :200
Actual   :404
 <Click to see difference>

    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
        ...

0 个答案:

没有答案