From f38b091ba318686f3328502e0984ccb475c61f45 Mon Sep 17 00:00:00 2001 From: Tim Yates Date: Fri, 19 Apr 2024 14:21:03 +0100 Subject: [PATCH 1/3] Allow RestAssured to test external servers RestAssured testing was tied to the local EmbeddedServer. This change allows setting `micronaut.test.server.url` to target an external server. Closes #1006 --- .../assured/RequestSpecificationFactory.java | 21 ++++++++++--- .../RestAssuredExternalServerTest.java | 30 +++++++++++++++++++ .../src/test/resources/logback.xml | 14 +++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 test-rest-assured/src/test/java/io/micronaut/test/rest/assured/RestAssuredExternalServerTest.java create mode 100644 test-rest-assured/src/test/resources/logback.xml diff --git a/test-rest-assured/src/main/java/io/micronaut/test/rest/assured/RequestSpecificationFactory.java b/test-rest-assured/src/main/java/io/micronaut/test/rest/assured/RequestSpecificationFactory.java index 5ce4f9fc3..739ed8821 100644 --- a/test-rest-assured/src/main/java/io/micronaut/test/rest/assured/RequestSpecificationFactory.java +++ b/test-rest-assured/src/main/java/io/micronaut/test/rest/assured/RequestSpecificationFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2022 original authors + * Copyright 2017-2024 original authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,20 +16,23 @@ package io.micronaut.test.rest.assured; import io.micronaut.context.annotation.Factory; +import io.micronaut.context.annotation.Property; import io.micronaut.context.annotation.Prototype; import io.micronaut.context.annotation.Requires; import io.micronaut.runtime.server.EmbeddedServer; import io.restassured.RestAssured; import io.restassured.specification.RequestSpecification; +import static io.micronaut.test.support.server.TestEmbeddedServer.PROPERTY; + /** * Helper class making it easier to create request specifications. * * @author gkrocher - * @since 3.4.0 + * @since 3.4.0 */ @Factory -@Requires(beans = EmbeddedServer.class) +@Requires(beans = EmbeddedServer.class) public class RequestSpecificationFactory { /** @@ -37,10 +40,20 @@ public class RequestSpecificationFactory { * @return A request specification for the current server. */ @Prototype - @Requires(beans = EmbeddedServer.class) + @Requires(beans = EmbeddedServer.class, missingProperty = PROPERTY) RequestSpecification requestSpecification(EmbeddedServer embeddedServer) { return RestAssured.given() .port(embeddedServer.getPort()); } + /** + * @param url The optional URL for an external service + * @return A request specification for the current server. + */ + @Prototype + @Requires(property = PROPERTY) + RequestSpecification requestSpecificationWithURL(@Property(name = PROPERTY) String url) { + return RestAssured.given() + .baseUri(url); + } } diff --git a/test-rest-assured/src/test/java/io/micronaut/test/rest/assured/RestAssuredExternalServerTest.java b/test-rest-assured/src/test/java/io/micronaut/test/rest/assured/RestAssuredExternalServerTest.java new file mode 100644 index 000000000..9400192e0 --- /dev/null +++ b/test-rest-assured/src/test/java/io/micronaut/test/rest/assured/RestAssuredExternalServerTest.java @@ -0,0 +1,30 @@ +package io.micronaut.test.rest.assured; + +import io.micronaut.context.annotation.Property; +import io.micronaut.test.extensions.junit5.annotation.MicronautTest; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.hasSize; + +@MicronautTest +@Property(name = "micronaut.test.server.url", value = "https://jsonplaceholder.typicode.com") +class RestAssuredExternalServerTest { + + @Test + void testSinglePost(RequestSpecification spec) { + spec + .when().get("/posts/1") + .then().statusCode(200) + .body("title", is("sunt aut facere repellat provident occaecati excepturi optio reprehenderit")); + } + + @Test + void testUsers(RequestSpecification spec) { + spec + .when().get("/users") + .then().statusCode(200) + .body("id", hasSize(10)); + } +} diff --git a/test-rest-assured/src/test/resources/logback.xml b/test-rest-assured/src/test/resources/logback.xml new file mode 100644 index 000000000..44b79c40d --- /dev/null +++ b/test-rest-assured/src/test/resources/logback.xml @@ -0,0 +1,14 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + From 832c34e0140a7f80c4f19550e04037bf3b21fc4a Mon Sep 17 00:00:00 2001 From: Tim Yates Date: Fri, 19 Apr 2024 14:27:06 +0100 Subject: [PATCH 2/3] Fix javadoc --- .../test/rest/assured/RequestSpecificationFactory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-rest-assured/src/main/java/io/micronaut/test/rest/assured/RequestSpecificationFactory.java b/test-rest-assured/src/main/java/io/micronaut/test/rest/assured/RequestSpecificationFactory.java index 739ed8821..1b1e0022d 100644 --- a/test-rest-assured/src/main/java/io/micronaut/test/rest/assured/RequestSpecificationFactory.java +++ b/test-rest-assured/src/main/java/io/micronaut/test/rest/assured/RequestSpecificationFactory.java @@ -47,8 +47,8 @@ RequestSpecification requestSpecification(EmbeddedServer embeddedServer) { } /** - * @param url The optional URL for an external service - * @return A request specification for the current server. + * @param url The optional URL for an external service + * @return A request specification for the external server. */ @Prototype @Requires(property = PROPERTY) From 3fa06dadb37a75c8cad2d3d4d0726d91764f163f Mon Sep 17 00:00:00 2001 From: Tim Yates Date: Fri, 19 Apr 2024 15:37:35 +0100 Subject: [PATCH 3/3] Re-run sonar to see if it stops lying