gpt4 book ai didi

java - 如何使用 Spring Mobile (spring-mobile-device) Junit 一个 RestController

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:51:02 24 4
gpt4 key购买 nike

我有一个带有设备(设备必须解析,我使用的是 spring-mobile-device)作为参数的 Rest Controller 。单元测试给了我一个状态 415。

代码如下

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> authenticationRequest(@RequestBody AuthenticationRequestDto authenticationRequest,
Device device) throws AuthenticationException {

Authentication authentication = this.authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
authenticationRequest.getUsername(), authenticationRequest.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authentication);

UserDetails userDetails = this.userDetailsService.loadUserByUsername(authenticationRequest.getUsername());

String token = this.tokenGenerator.generateToken(userDetails, device);

return ResponseEntity.ok(new AuthenticationResponseDto(token));
}

单元测试

    ResultActions res = mockMvc.perform(post("/auth", authentication, device).contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(authentication)));
res.andExpect(status().isOk());

最佳答案

基本上我的配置有误。必须以与生产配置相同的方式配置 Web 配置以进行测试,但在语法上有所不同。好吧,我通过这个问题学到了很多关于 MockMVC 配置的知识。

如果您想使用 spring mobile 进行单元测试,这里是解决方案。

头等舱

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebTestConfig.class})
@WebAppConfiguration
public class WebTestConfigAware {

@Autowired
private WebApplicationContext context;

protected MockMvc mockMvc;

@Autowired
private FilterChainProxy springSecurityFilterChain;

@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
DeviceResolverRequestFilter deviceResolverRequestFilter = new DeviceResolverRequestFilter();

mockMvc = MockMvcBuilders.webAppContextSetup(context)
.addFilters(this.springSecurityFilterChain, deviceResolverRequestFilter).build();
}

}

二等舱

@Configuration
@EnableWebMvc
@Import({RootTestConfig.class, WebCommonSecurityConfig.class})
public class WebTestConfig extends WebMvcConfigurerAdapter{


@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new ServletWebArgumentResolverAdapter(new DeviceWebArgumentResolver()));
argumentResolvers.add(new SitePreferenceHandlerMethodArgumentResolver());
}
}

和测试类

public class AuthenticationControllerTest extends WebTestConfigAware {

@Test
public void testAuthenticationRequest() throws Exception {
AuthenticationRequestDto authentication = new AuthenticationRequestDto();
authentication.setUsername("admin");
authentication.setPassword("Test1234");

String jsonAuthentication = TestUtil.convertObjectToJsonString(authentication);

ResultActions res = mockMvc.perform(post("/auth")
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE).content(jsonAuthentication));

res.andExpect(status().isOk());

}

关于java - 如何使用 Spring Mobile (spring-mobile-device) Junit 一个 RestController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42584452/

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