我们刚刚升级到Spring Boot 1.4并正在使用Spring Boot测试改进。我们有以下形式的集成测试:
RunWith(SpringRunner.class)
@SpringBootTest(classes = {
ApplicationConfiguration.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ServiceIntegrationTest {
private static final String AEndpoint = Constants.A_REST_CONTROLLER_ENDPOINT;
private static final String TEndpoint = Constants.T_REST_CONTROLLER_ENDPOINT;
@Autowired
private TestRestTemplate testRestTemplate;
public void postJsonNode(String postUrl, JsonNode requestBody) {
HttpEntity<JsonNode> request = new HttpEntity<JsonNode>(requestBody, this.headers);
ResponseEntity<String> post = this.testRestTemplate.postForEntity(postUrl, request, String.class);
assertThat(post.getStatusCode(), equalTo(HttpStatus.CREATED));
}
这些类型的测试通过但是由于ObjectMapper被弃用而被替换为预先连线的_halMapper(我称之为bean布线问题),Application无法启动。
***************************
APPLICATION FAILED TO START
***************************
Description:
Constructor in org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration required a single bean, but 2 were found:
- objectMapper: defined by method 'objectMapper' in com.service.ApplicationConfiguration
- _halObjectMapper: defined in null
申请类:
public class Application {
public static void main(String[] args) {
SpringApplication.run(ApplicationConfiguration.class, args);
}
}
ApplicationConfiguration类:
@SpringBootApplication(scanBasePackages = "com.service")
public class ApplicationConfiguration {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
return objectMapper;
}
}
有没有办法通过集成测试来测试应用程序启动?