Spring 使用 mockMvc 的 JUnit 测试
Spring JUnit testing using mockMvc
我正在尝试使用 spring 和 mockito 对我的休息控制器进行单元测试。这是我的主要控制器方法。
public static final String AUTHENTICATE_USER_URI = "/user/authenticate";
private static final Logger logger = LoggerFactory.getLogger(RestfulController.class);
@Autowired
User user;
@Autowired
AuthenticationService authService;
@CrossOrigin
@RequestMapping(value = (AUTHENTICATE_USER_URI), method = RequestMethod.GET, produces = "application/json")
private User getUserAuthenticationDetails(
@RequestParam("name") String name,
@RequestParam("password") String password) throws NamingException,
AuthenticationException {
boolean isAuth = true;
// checks user credentials
isAuth = authService.isAuthenticated(name, password);
if (isAuth) {
logger.debug("User is authenticated");
user = authService.getUserDetails(user, name);
// // persistUserInDB(user);
return user;
} else {
logger.debug("User is not authenticated");
return null;
}
}
这是我的 JUnit 测试:
@RunWith(MockitoJUnitRunner.class)
public class RestfulControllerTest {
private static final String AUTHENTICATE_USER_TEST_URI = "http://localhost:8086/LDAPAuthenticationApp/user/authenticate";
private MockMvc mockmvc;
@Mock
private AuthenticationService authService;
@Before
public void setUp() {
mockmvc = MockMvcBuilders.standaloneSetup(new RestfulController())
.setHandlerExceptionResolvers(exceptionResolver())
.setMessageConverters(messageConverters())
.build();
}
private MappingJackson2HttpMessageConverter messageConverters(){
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
return converter;
}
private HandlerExceptionResolver exceptionResolver() {
// exceptionResolver implementation here
}
// success test case
@Test
public void testGetUserAuthenticationDetails() throws Exception {
String[] name={"amol84","arpan","juhi"};
String value="pegasus84";
when(authService.isAuthenticated(any(String.class), any(String.class))).thenReturn(true);
when(authService.getUserDetails(any(User.class),any(String.class))).thenReturn(any(User.class));
for(int i=0;i<3;i++){
mockmvc.perform(get(AUTHENTICATE_USER_TEST_URI).param("name", name[i]).param("password", value))
.andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
}
verify(authService, atMost(3)).isAuthenticated(any(String.class), any(String.class));
}
在输出响应中,测试失败,因为它收到 404 错误,但需要 200 成功代码。我可能做错了什么,因为我相信我已经正确设置了独立配置。为什么 URI 没有正确映射到方法?请注意,对于来自应用程序前端的相同 URI,它可以正常工作。这是我使用 Postman 工具针对 chrome 测试过的正确 200 响应的完整 URI:
http://localhost:8086/LDAPAuthenticationApp/user/authenticate?name=amol84&password=pegasus84
也许你应该试试这个 url:
private static final String AUTHENTICATE_USER_TEST_URI = "/user/authenticate";
我正在尝试使用 spring 和 mockito 对我的休息控制器进行单元测试。这是我的主要控制器方法。
public static final String AUTHENTICATE_USER_URI = "/user/authenticate";
private static final Logger logger = LoggerFactory.getLogger(RestfulController.class);
@Autowired
User user;
@Autowired
AuthenticationService authService;
@CrossOrigin
@RequestMapping(value = (AUTHENTICATE_USER_URI), method = RequestMethod.GET, produces = "application/json")
private User getUserAuthenticationDetails(
@RequestParam("name") String name,
@RequestParam("password") String password) throws NamingException,
AuthenticationException {
boolean isAuth = true;
// checks user credentials
isAuth = authService.isAuthenticated(name, password);
if (isAuth) {
logger.debug("User is authenticated");
user = authService.getUserDetails(user, name);
// // persistUserInDB(user);
return user;
} else {
logger.debug("User is not authenticated");
return null;
}
}
这是我的 JUnit 测试:
@RunWith(MockitoJUnitRunner.class)
public class RestfulControllerTest {
private static final String AUTHENTICATE_USER_TEST_URI = "http://localhost:8086/LDAPAuthenticationApp/user/authenticate";
private MockMvc mockmvc;
@Mock
private AuthenticationService authService;
@Before
public void setUp() {
mockmvc = MockMvcBuilders.standaloneSetup(new RestfulController())
.setHandlerExceptionResolvers(exceptionResolver())
.setMessageConverters(messageConverters())
.build();
}
private MappingJackson2HttpMessageConverter messageConverters(){
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
return converter;
}
private HandlerExceptionResolver exceptionResolver() {
// exceptionResolver implementation here
}
// success test case
@Test
public void testGetUserAuthenticationDetails() throws Exception {
String[] name={"amol84","arpan","juhi"};
String value="pegasus84";
when(authService.isAuthenticated(any(String.class), any(String.class))).thenReturn(true);
when(authService.getUserDetails(any(User.class),any(String.class))).thenReturn(any(User.class));
for(int i=0;i<3;i++){
mockmvc.perform(get(AUTHENTICATE_USER_TEST_URI).param("name", name[i]).param("password", value))
.andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
}
verify(authService, atMost(3)).isAuthenticated(any(String.class), any(String.class));
}
在输出响应中,测试失败,因为它收到 404 错误,但需要 200 成功代码。我可能做错了什么,因为我相信我已经正确设置了独立配置。为什么 URI 没有正确映射到方法?请注意,对于来自应用程序前端的相同 URI,它可以正常工作。这是我使用 Postman 工具针对 chrome 测试过的正确 200 响应的完整 URI:
http://localhost:8086/LDAPAuthenticationApp/user/authenticate?name=amol84&password=pegasus84
也许你应该试试这个 url:
private static final String AUTHENTICATE_USER_TEST_URI = "/user/authenticate";