Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring启动测试,从1.3迁移到1.5_Spring_Spring Boot_Spring Test - Fatal编程技术网

Spring启动测试,从1.3迁移到1.5

Spring启动测试,从1.3迁移到1.5,spring,spring-boot,spring-test,Spring,Spring Boot,Spring Test,我将我的spring boot项目从1.3.x更新为1.5.2。测试框架已经“改变”,我正在尝试迁移代码。RestTemplate中的响应状态代码应该是401,但是当我将代码更改为新的“结构”时,我得到了404,NotFound。你知道可能会遗漏什么吗 旧代码: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = ApiAuthServerApplication.class) @Web

我将我的spring boot项目从1.3.x更新为1.5.2。测试框架已经“改变”,我正在尝试迁移代码。RestTemplate中的响应状态代码应该是401,但是当我将代码更改为新的“结构”时,我得到了404,NotFound。你知道可能会遗漏什么吗

旧代码:

@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(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=ApiAuthServerApplication.class)
@WebAppConfiguration
@IntegrationTest(“{server.port:0,server.address:localhost}”)
公共类测试{
@值(${local.server.port}”)
专用int端口;
私有RestTemplate=新TestRestTemplate();
@试验
public void clientsEndpointProtected(){
ResponseEntity response=template.getForEntity(“http://localhost:"
+端口+“/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());
    }
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes=ApiAuthServerApplication.class,webEnvironment=SpringBootTest.webEnvironment.RANDOM\u端口)
公共类测试{
@本地服务器端口
专用int端口;
私有TestRestTemplate=新TestRestTemplate();
@试验
public void clientsEndpointProtected(){
ResponseEntity response=template.getForEntity(“http://localhost:"
+端口+“/uaa/api/v1/oauth/clients”,String.class);
assertEquals(HttpStatus.UNAUTHORIZED,response.getStatusCode());
}
}

还试图
@Autowire
测试RESTTemplate,并在请求中省略主机名和端口。

使用WebEnvironment.RANDOM\u端口时,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());
  }
}
TestRestTemplate的自动连线。 @RunWith(SpringRunner.class) @SpringBootTest(类=ApiAuthServerApplication.class,webEnvironment= SpringBootTest.WebEnvironment.RANDOM_端口) 公共类测试{ @自动连线 私有TestRestTemplate=新TestRestTemplate(); @试验 public void clientsEndpointProtected(){ 响应性响应= template.getForEntity(“/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 {

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

您只需
@Autowire
TestRestTemplate而无需创建新实例,然后Spring Boot应该正确修复URL,您就可以调用
/uaa/api/v1/oauth/clients
。我以前尝试过这一点,在响应代码中得到与之前相同的结果(404未找到)
@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());
  }
}