@SpringBootTest在通过maven运行测试时意外行为(surefire)

时间:2016-09-27 03:31:11

标签: spring-boot spring-security-oauth2 spring-test spring-test-mvc

我有一个Spring Boot 1.4.1项目,这是我的测试类的组织:

  

在src / test / java

中打包com.service
RunWith(SpringRunner.class)
@SpringBootTest(classes = {
    ApplicationConfiguration.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IntegrationTest {

 @Value("${bind.password}")
private String PASSWORD;

@Value("${service.client.name}")
private String CLIENT_NAME;

@Value("${service.client.secret}")
private String CLIENT_PASSWORD;

public JsonNode getAgentRequest(String agentName) {
    JsonNode requestBody = mapper.createObjectNode().put("agentName", agentName).put("agentType", "Executioner")
            .put("location", "Seattle").put("agentApiEndpoint", "http://localhost:9090");
    return requestBody;
}



public String getOAuthToken() {
    MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>();
    request.set("username", TestConstants.USERNAME);
    request.set("password", PASSWORD);
    request.set("grant_type", "password");
    @SuppressWarnings("unchecked")
    Map<String, Object> token = this.testRestTemplate.withBasicAuth(CLIENT_NAME, CLIENT_PASSWORD)
            .postForObject(Constants.OAUTH_ENDPOINT, request, Map.class);
    assertNotNull("OAuth token not returned: " + token, token.get("access_token"));
    return token.get("access_token").toString();
}

public MultiValueMap<String, String> getAuthAndJsonHeaders() {
    MultiValueMap<String, String> headerMapWithAuth = new LinkedMultiValueMap<String, String>();
    headerMapWithAuth.set("Authorization", "Bearer " + getOAuthToken());
    return headerMapWithAuth;

@Test
public void postJsonNode(String postUrl, MultiValueMap<String, String> headerMap, JsonNode requestBody) {
    HttpEntity<JsonNode> request = new HttpEntity<JsonNode>(requestBody, headerMap);
    ResponseEntity<String> postResponseEntity = this.testRestTemplate.postForEntity(postUrl, request, String.class);
    assertThat(postResponseEntity.getStatusCode(), equalTo(HttpStatus.CREATED));

}
}

我有另一个包裹:

  

src / test / java中的com.service.authentication

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {
    ApplicationConfiguration.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class OAuth2IntegrationTest {

@Value("${bind.password}")
private String PASSWORD;

@Value("${service.client.name}")
private String CLIENT_NAME;

@Value("${service.client.secret}")
private String CLIENT_PASSWORD;

@Autowired
private TestRestTemplate testRestTemplate;

@Test
public void testOAuthAccessTokenIsReturned() {
    MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>();
   request.set("username", TestConstants.USERNAME);
    request.set("password", PASSWORD);
    request.set("grant_type", "password");
    @SuppressWarnings("unchecked")
    Map<String, Object> token = this.testRestTemplate.withBasicAuth(CLIENT_NAME, CLIENT_PASSWORD)
            .postForObject(Constants.OAUTH_ENDPOINT, request, Map.class);
    assertNotNull("OAuth token not returned: " + token, token.get("access_token"));
}
}

运行时,所有使用OAuth令牌的测试都会在OAuth2IntegrationTest.java中传递:

  

mvn install

使用OAuth令牌的所有测试在IntegrationTest.java中失败,但有以下异常:

  

org.springframework.web.client.ResourceAccessException:对于&#34; http://localhost:59541/oauth/token&#34;的POST请求的I / O错误:由于服务器身份验证而无法在流模式下重试;嵌套异常是java.net.HttpRetryException:由于服务器身份验证无法在流模式下重试

IntegrationTest.java中的所有测试以及OAuth2IntegrationTest.java都传递给STS 3.8.1。 如果我将失败的测试从IntegrationTest.java移到OAuth2IntegrationTest.java,它们就会通过。 如果我将OAuth2IntegrationTest.java移动到com.service,一切都会通过,包括IntegrationTest.java中的测试。我正在使用:

  

maven 3.3.9,其中有确定的2.18.1

我从前面的问题中得到了一些在包中移动文件的提示: @SpringBootTest does not autowire configurations in a deeper package

我已经使用原始设置单独运行STS上的每个测试,它们都通过了。他们只在maven上失败了。我的项目设置与ApplicationConfiguration.java的链接完全没有什么特别之处。我不知道如何解决这个问题,看起来像工具/框架问题

0 个答案:

没有答案