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

Rest controller test with @WebMvcTest, Could not instantiate JAXBContext for PagedResources

我有一些失败的测试 Error creating bean with name 'entityManagerFactory' 这个答案为我解决了 但是 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。我应该怎么做才能最好地解决这个问题?

控制器测试class出现异常:

@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);
    }
}

我的 pom 依赖于 jaxb-api

 <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>
</dependencies>

如果您使用 Java 9 或更高版本,您需要添加以下 VM 选项才能启动 tests/application

--add-modules
java.xml.bind