我有一些测试未通过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);
}
}
我对jaxb-api的pom依赖
<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>
答案 0 :(得分:0)
如果使用Java 9或更高版本,则需要添加以下VM选项以启动测试/应用程序
--add-modules
java.xml.bind