使用@WebMvcTest 进行休息控制器测试,无法为 PagedResources

我有一些测试失败了,Error creating bean with name 'entityManagerFactory'这个答案为我解决了https://stackoverflow.com/a/47504698/6945345但破坏了我的控制器测试Could not instantiate JAXBContext for class [class org.springframework.hateoas.PagedResources]: Implementation of JAXB-API has not been found on module path or classpath.这是因为我认为@WebMvcTest没有选择 JAXB-API。我该怎么做才能以最佳方式解决此问题?


给出异常的控制器测试类:


@RunWith(SpringRunner.class)

@WebMvcTest(BiodiversityController.class)

@Import(SpecieResourceAssembler.class)

public class BiodiversityControllerTest {


    @MockBean

    private SpecieService specieService;


    @Autowired

    private MockMvc mockMvc;


    @Autowired

    private SpecieResourceAssembler specieResourceAssembler;


    @Before

    public void setup() {

        mockMvc = MockMvcBuilders.standaloneSetup(new BiodiversityController(specieService, specieResourceAssembler))

            .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())

            .build();

    }


    @Test

    public void getAllSpecies_ShouldReturnSpecies() throws Exception {

        PageRequest pageRequest = PageRequest.of(0, 20);

        given(specieService.getAllSpecies(pageRequest)).willReturn(new PageImpl<>(

            Collections.singletonList(createAnimaliaOrestias()), pageRequest, 1));


        mockMvc.perform(MockMvcRequestBuilders.get("/species?page=0&size=20"))

            .andExpect(status().isOk())

            .andExpect(jsonPath("$.content", hasSize(1)))

            .andExpect(jsonPath("$.content.[0].name").value(NAME_ORESTIAS));


        verify(specieService).getAllSpecies(pageRequest);

    }

}


沧海一幻觉
浏览 224回答 1
1回答

ABOUTYOU

如果您使用 Java 9 或更高版本,则需要添加以下 VM 选项来启动测试/应用程序--add-modulesjava.xml.bind
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java