2016-11-17 122 views
8

我一直在努力与此一段时间了。 我想用restAssured来测试我的SpringBoot REST应用程序。测试春季开机休息应用程序restAssured

虽然它看起来像集装箱正常旋转起来,放心(和别的似乎已经深入到它的问题。

所有的时间我得到拒绝连接例外。

java.net.ConnectException: Connection refused 

at java.net.PlainSocketImpl.socketConnect(Native Method) 
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
at java.net.Socket.connect(Socket.java:589) 
... 

我的测试类:

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
public class SizesRestControllerIT { 

    @Autowired 
    private TestRestTemplate restTemplate; 

    @Test 
    public void test() { 
     System.out.println(this.restTemplate.getForEntity("/clothes", List.class)); 
    } 

    @Test 
    public void test2() throws InterruptedException { 
     given().basePath("/clothes").when().get("").then().statusCode(200); 
    } 

} 

现在对于怪异的一部分,test通行证和打印什么应该,但test2越来越ç连锁拒绝例外。

任何想法这个设置有什么问题?

回答

21

我来回答这个问题,我自己..

在花费时间更多的量之后事实证明,TestRestTemplate已经知道,并设置适当的端口。 RestAssured没有...

因此,我得到了一个点,低于测试运行没有任何问题。

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
public class SizesRestControllerIT { 

    @LocalServerPort 
    int port; 

    @Test 
    public void test2() throws InterruptedException { 
     given().port(port).basePath("/clothes").get("").then().statusCode(200); 
    } 

} 

我可以发誓,我以前试过这样做......但我想我没有使用一些其他注释本...

-1

传递"/clothes"作为参数传递给get()方法应该可以解决

@Test 
public void test2() throws InterruptedException { 
    when(). 
     get("/clothes"). 
    then(). 
     statusCode(200); 
} 
+1

不幸的是这并没有改变任何东西 – klubi

2

你一些非标准端口上运行可能是问题? 有你在尝试这个你

@Before public static void init(){ RestAssured.baseURI = "http://localhost"; // replace as appropriate RestAssured.port = 8080; }

+0

'SpringBootTest.WebEnvironment.RANDOM_PORT'导致应用不同的端口则每次 – klubi

+0

可能是上运行, ' @Value(” $ {local.server.port}“) int port; (){ RestAssured.baseURI =“http:// localhost”; //根据需要替换 RestAssured.port = port; } ' – user3169330

0

简单:基于

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT) 
public class CommonScenarioTest { 

    @BeforeClass 
    public static void setup() { 
     RestAssured.baseURI = "http://localhost/foo"; 
     RestAssured.port = 8090; 
    } 
0

https://stackoverflow.com/users/2838206/klubi回答,并且不为每个请求设置端口:

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = 
SpringBootTest.WebEnvironment.RANDOM_PORT) 
public class SizesRestControllerIT { 

    @LocalServerPort 
    int port; 

    @Before 
    public void setUp() { 
     RestAssured.port = port; 
    } 

    @Test 
    public void test2() throws InterruptedException { 
     given().basePath("/clothes").get("").then().statusCode(200); 
    } 
} 
0

我会推荐次使用@WebMvcTest的情况下,所有你需要的是有放心模拟MVC依赖性:

<dependency> 
      <groupId>com.jayway.restassured</groupId> 
      <artifactId>spring-mock-mvc</artifactId> 
      <version>${rest-assured.version}</version> 
      <scope>test</scope> 
</dependency> 

使用的@SpringBootTest测试只是一个控制器的开销,所有多余的豆类,如@Component@Service等,将被创建并且一个完整的HTTP服务器将被启动。欲了解更多详情: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-autoconfigured-mvc-tests;

@RunWith(SpringRunner.class) 
    @WebMvcTest(value = SizesRestController.class) 
    public class SizesRestControllerIT { 

    @Autowired 
    private MockMvc mvc; 

    @Before 
    public void setUp() { 
     RestAssuredMockMvc.mockMvc(mvc); 
    } 

    @Test 
    public void test() { 
     RestAssuredMockMvc.given() 
      .when() 
      .get("/clothes") 
      .then() 
      .statusCode(200); 
     // do some asserts 
    } 
} 
0

我有同样的问题,服务器在端口34965(不是8080)上运行应用程序。

这解决了我的问题:

@Autowired 
ServerProperties serverProperties; 

@Autowired 
Environment environment; 

public String getPath() { 
    final int port = environment.getProperty("local.server.port", Integer.class); 

    return "http://localhost:" + port; 
} 

@Before 
public void setUp() throws Exception { 
    RestAssured.baseURI = getPath(); 
} 

@Test 
public void testResponse(){ 
    response = get("/books"); 
}