简体   繁体   中英

Error: java.lang.IllegalArgumentException: URI must not be null when using RestAssured in SpringBootTest aginst the RestTemplate

This the BaseTest abstract class where the configuration is being set :

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

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

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

The test class is very simple :

public class TestIT extends BaseIntegration{//Test Code}

RestClient created using RestAssured :

public static TestResponse postResponse(TestRequest request) {

    return given().log().all()
            .contentType(ContentType.JSON)
            .body(request)
            .when()
            .log().all()
            .post("/weather")
            .as(TestResponse.class);

}

When the test are running in teamcity thsi is what the error log says :

java.lang.IllegalArgumentException: URI must not be null
at org.springframework.util.Assert.notNull(Assert.java:134) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.util.UriComponentsBuilder.fromUriString(UriComponentsBuilder.java:189) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.util.DefaultUriTemplateHandler.initUriComponentsBuilder(DefaultUriTemplateHandler.java:114) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.util.DefaultUriTemplateHandler.expandInternal(DefaultUriTemplateHandler.java:103) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.util.AbstractUriTemplateHandler.expand(AbstractUriTemplateHandler.java:106) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:612) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:407) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]

I am not using the RestTemplate or TestRestTemplate. There is another test that is using that and that test is working fine. I am unable to understand what can be causing the problem Much help is appreciated.Thanks.

EDIT: Seems like the service endpoint is using RestTemplate

restTemplate.postForEntity(url, body, TestResponse.class);

On calling the endpoint my test are failing. I am thinking that there are some compatilibily issue with RestTemplate and RestAssured lib.

You already has .log().all() duplicated.

return given().log().all()
        .contentType(ContentType.JSON)
        .body(request)
        .when()            
        .post("/weather")
        .andReturn()
        .as(TestResponse.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM