gpt4 book ai didi

java - 使用mockMvc进行Spring JUnit测试

转载 作者:行者123 更新时间:2023-11-30 07:26:24 26 4
gpt4 key购买 nike

我正在尝试使用 spring 和mockito 对我的休息 Controller 进行单元测试。这是我的主要 Controller 方法。

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,它可以正常工作。以下是我使用 Chrome 的 Postman 工具测试过的正确 200 响应的完整 URI:

http://localhost:8086/LDAPAuthenticationApp/user/authenticate?name=amol84&password=pegasus84

最佳答案

也许你应该尝试使用这个网址:

private static final String AUTHENTICATE_USER_TEST_URI = "/user/authenticate";

关于java - 使用mockMvc进行Spring JUnit测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36764988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com