Dav*_*ave 7 java spring-mvc spring-mvc-test spring-boot
在春季启动应用程序,我有两个的POJO,Foo和Bar和BarToFooConverter,它看起来像:
@Component
public class BarToFooConverter implements Converter<Bar, Foo> {
@Override
public Foo convert(Bar bar) {
return new Foo(bar.getBar());
}
}
Run Code Online (Sandbox Code Playgroud)
我还有一个使用转换器的控制器:
@RestController("test")
public class TestController {
@Autowired
private ConversionService conversionService;
@RequestMapping(method = RequestMethod.PUT)
@ResponseBody
public Foo put(@RequestBody Bar bar) {
return conversionService.convert(bar, Foo.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我想测试这个控制器@WebMvcTest,例如:
@WebMvcTest
@RunWith(SpringRunner.class)
public class TestControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void test() throws Exception {
mockMvc.perform(
put("/test")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"bar\":\"test\"}"))
.andExpect(status().isOk());
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行这个时,我发现我BarToFooConverter没有注册ConversionService:
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [com.example.demo.web.Bar] to type [com.example.demo.web.Foo]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:324)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:206)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:187)
at com.example.demo.web.TestController.put(TestController.java:15)
Run Code Online (Sandbox Code Playgroud)
这似乎是有道理的,因为根据Javadoc:
使用此批注将禁用完全自动配置,而是仅应用与MVC测试相关的配置(即@ Controller,@ ControllerAdvice,@ JsonComponent Filter,WebMvcConfigurer和HandlerMethodArgumentResolver bean,但不包括@Component,@ Service或@Repository bean).
但是,参考指南略有不同,说它@WebMvcTest 确实包括Converters:
@WebMvcTest自动配置Spring MVC基础结构并将扫描的bean限制为@ Controller,@ ControllerAdvice,@ JsonComponent,Converter,GenericConverter,Filter,WebMvcConfigurer和HandlerMethodArgumentResolver.使用此批注时,不会扫描常规@Component bean.
这里的参考指南似乎不正确 - 或者我正在注册我的Converter错误?
我也试过ConversionService在我的测试中嘲笑:
@WebMvcTest
@RunWith(SpringRunner.class)
public class TestControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ConversionService conversionService;
@Test
public void test() throws Exception {
when(conversionService.convert(any(Bar.class), eq(Foo.class))).thenReturn(new Foo("test"));
mockMvc.perform(
put("/test")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"bar\":\"test\"}"))
.andExpect(status().isOk());
}
}
Run Code Online (Sandbox Code Playgroud)
但现在Spring抱怨我的模拟ConversionService覆盖了默认模拟:
Caused by: java.lang.IllegalStateException: @Bean method WebMvcConfigurationSupport.mvcConversionService called as a bean reference for type [org.springframework.format.support.FormattingConversionService] but overridden by non-compatible bean instance of type [org.springframework.core.convert.ConversionService$$EnhancerByMockitoWithCGLIB$$da4e303a]. Overriding bean of same name declared in: null
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.obtainBeanInstanceFromFactory(ConfigurationClassEnhancer.java:402)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
...
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想使用我的原始方法,在我的测试中使用真正的Converter而不是模拟ConversionService,但是@WebMvcTest为了限制启动的组件的范围,所以我也尝试includeFilter在@WebMvcTest注释中使用an :
@WebMvcTest(includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.example.demo.web.Bar*"))
Run Code Online (Sandbox Code Playgroud)
但它仍然失败,原始的'无转换器发现能够转换...'错误消息.
这感觉就像是一个非常常见的要求 - 我错过了什么?
您可以通过带注释的方法手动注册转换器@Before。您所要做的就是注入GenericConversionService并调用addConverter(new BarToFooConverter())以使转换器可解析。在这种情况下,您可以摆脱模拟部分。您的测试可能如下所示:
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest
@RunWith(SpringRunner.class)
public class TestControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private GenericConversionService conversionService;
@Before
public void setup() {
conversionService.addConverter(new BarToFooConverter());
}
@Test
public void test() throws Exception {
mockMvc.perform(
put("/test")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"bar\":\"test\"}"))
.andExpect(status().isOk());
}
}
Run Code Online (Sandbox Code Playgroud)
替代解决方案:Spring Boot 从1.4.0版本开始提供了一系列与测试相关的自动配置,其中一个自动配置是@AutoConfigureMockMvc配置MockMvc与注入的转换器组件配合良好的组件。
| 归档时间: |
|
| 查看次数: |
3506 次 |
| 最近记录: |