用restAssured测试spring boot rest应用程序 [英] testing spring boot rest application with restAssured

查看:747
本文介绍了用restAssured测试spring boot rest应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决这个问题。
我想使用restAssured来测试我的SpringBoot REST应用程序。



虽然看起来容器旋转正常,但请放心(其他任何东西似乎都是我遇到连接拒绝例外。



> java.net.ConnectException:连接拒绝

java.net.PlainSocketImpl.socketConnect(本机方法)
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_PO) RT)
公共类SizesRestControllerIT {

@Autowired
private TestRestTemplate restTemplate;

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

@Test
public void test2()抛出InterruptedException {
given()。basePath(/ clothes)。when()。get( )。然后()的StatusCode(200)。
}

}

现在对于奇怪的部分, test 传递并打印它应该的内容,但是 test2 正在获得Connection拒绝异常。



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

解决方案

我会自己回答这个问题..



在花了额外的时间之后,事实证明 TestRestTemplate 已经知道并设置了正确的端口。
RestAssured没有......



有了这个,我得到了以下测试运行没有任何问题。

  @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()抛出InterruptedException {
given()。basePath(/ clothes)。get()。then ().statusCode(200);
}

}

我可以发誓我试过做以前这种方式...但我想我确实使用了其他一些注释......


I've been struggling with this for some time now. I'd like to use restAssured to test my SpringBoot REST application.

While it looks like container spins up properly, rest assured (and anything else seems to have problems reaching out to it.

All the time I'm getting Connection refused exception.

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)
...

my test class:

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

}

and now for the weird part, test passes and prints what it should, but test2 is getting Connection refused exception.

Any ideas what is wrong with this setup?

解决方案

I'll answer this question myself..

After spending additional amount of time on it it turned out that TestRestTemplate already knows and sets proper port. RestAssured does not...

With that I got to a point where below test runs without any issues.

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

}

I could have sworn I tried doing it this way previously... But I guess I did use some other annotations with this...

这篇关于用restAssured测试spring boot rest应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆