Spring Boot测试,从1.3迁移到1.5

时间:2017-04-19 10:52:46

标签: spring spring-boot spring-test

我将spring-boot项目从1.3.x更新为1.5.2。测试框架已经改变了#34;我正在尝试迁移我的代码。来自RestTemplate的响应状态代码应该是401,但是当我将代码更改为新的"结构"我找到了404,没找到。什么可能遗漏的想法?

旧代码:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ApiAuthServerApplication.class)
@WebAppConfiguration
@IntegrationTest("{server.port:0, server.address:localhost}")
public class ApiEndpointTests {

    @Value("${local.server.port}")
    private int port;

    private RestTemplate template = new TestRestTemplate();

    @Test
    public void clientsEndpointProtected() {
        ResponseEntity<String> response = template.getForEntity("http://localhost:"
                + port + "/uaa/api/v1/oauth/clients", String.class);
        assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());            
    }
}

我尝试过的新代码:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApiAuthServerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApiEndpointTests {

    @LocalServerPort
    private int port;

    private TestRestTemplate template = new TestRestTemplate();

    @Test
    public void clientsEndpointProtected() {
        ResponseEntity<String> response = template.getForEntity("http://localhost:"
                + port + "/uaa/api/v1/oauth/clients", String.class);
        assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
    }
}

还尝试@Autowire TestRestTemplate并省略请求中的主机名和端口。

2 个答案:

答案 0 :(得分:2)

当您使用WebEnvironment.RANDOM_PORT时,sprint测试框架将处理主机和端口的配置和设置。因此,您可以删除主机和端口的详细信息。您还应该在TestRestTemplate上使用@Autowired注释。

@Autowired for the TestRestTemplate.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApiAuthServerApplication.class, webEnvironment = 
SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApiEndpointTests {

  @Autowired
  private TestRestTemplate template = new TestRestTemplate();

  @Test
  public void clientsEndpointProtected() {
    ResponseEntity<String> response = 
    template.getForEntity("/uaa/api/v1/oauth/clients", String.class);
    assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
  }
}

答案 1 :(得分:1)

想出来。

使用此代码时:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApiAuthServerApplication.class, webEnvironment = 
SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApiEndpointTests {

  @Autowired
  private TestRestTemplate template;

  @Test
  public void clientsEndpointProtected() {
    ResponseEntity<String> response = 
    template.getForEntity("/uaa/api/v1/oauth/clients", String.class);
    assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
  }
}

我还需要删除/uaa,因为这是上下文路径。我想TestRestTemplate也会自动包含它。所以最终的代码有效:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApiAuthServerApplication.class, webEnvironment = 
SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApiEndpointTests {

  @Autowired
  private TestRestTemplate template;

  @Test
  public void clientsEndpointProtected() {
    ResponseEntity<String> response = 
    template.getForEntity("/api/v1/oauth/clients", String.class);
    assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
  }
}