栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

无法针对REST层运行单元测试

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

无法针对REST层运行单元测试

我可以使用以下代码运行测试:

@RunWith(SpringRunner.class)//@WebMvcTest remove @WebMvcTest//add SpringBootTest@SpringBootTest(classes = JhUnittestRestApp.class)public class FlightResourceTest {    //@Autowired remove anotation    private MockMvc mockMvc;    @MockBean    private FlightService flightService;    @Autowired    private MappingJackson2HttpMessageConverter jacksonMessageConverter;    @Autowired    private PageableHandlerMethodArgumentResolver pageableArgumentResolver;    @Autowired    private ExceptionTranslator exceptionTranslator;    @Before    public void setup() {        //initialize the bean        MockitoAnnotations.initMocks(this);        final FlightResource flightResource = new FlightResource(flightService);        this.mockMvc = MockMvcBuilders.standaloneSetup(flightResource) .setCustomArgumentResolvers(pageableArgumentResolver) .setControllerAdvice(exceptionTranslator) .setConversionService(createFormattingConversionService()) .setMessageConverters(jacksonMessageConverter).build();    }    @Test    public void testCreate() throws Exception {        FlightDTO expected = new FlightDTO();        ReflectionTestUtils.setField(expected, "id", 1L);        when(flightService.save(any(FlightDTO.class))).thenReturn(expected);        FlightDTO flightDto = new FlightDTO();        flightDto.setNumber("CAI-123400");        //update the url to /api/flights so that the test can pass         this.mockMvc.perform(post("/api/flights") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(flightDto))) .andDo(print()) .andExpect(status().isCreated()) .andExpect(header().string("Location", endsWith("/api/flights/1")));    }}

您的FlightControllerTest在您的springboot-no-jhipster项目中运行,因为根据@SpringBoot的文档,该项目的主类使用@SpringBoot进行了注释。

The @SpringBootApplication annotation is equivalent to using:@Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes

由于JHipster需要的配置不仅仅是默认配置,因此JHipster并未使用@SpringBootApplication,正如您在项目中看到的那样。完全可以,并且没有问题。

另一方面,测试的错误消息是说它无法检测到@SpringBootConfiguration。还有其他注释,例如@ContextConfiguration或@SpringBootTest,它们是rocomandet可以用于测试的。实际上,主配置类中的注释与测试注释之间存在一些不一致之处,请参见此处或此处。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/403001.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号