Reputation: 855
I want to use Spring Cloud Contract to produce my contracts and verify them. I want to use Spring WebFlux and Junit5. This is my Controller:
@RestController
@Slf4j
public class HelloWorldPortRESTAdapter implements HelloWorldPort {
@GetMapping(value = "/hello-world", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Override
public Mono<String> helloWorld() {
return Mono.just("Hello World!");
}
}
This is cloud contract maven plugin configuration:
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<basePackageForTests>com.example.feedproviderapi.contract</basePackageForTests>
<testFramework>JUNIT5</testFramework>
<testMode>EXPLICIT</testMode>
</configuration>
</plugin>
But i don't know how base test class should look like. I tried this:
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BaseTestClass {
@LocalServerPort
private int port;
@BeforeEach
void setup(){
RestAssured.baseURI = "http://localhost:" + this.port;
}
}
When I run mvn clean install
, it returns java.net.ConnectException: Connection refused (Connection refused)
Then I changed testMode
property in maven plugin to WEBTESTCLIENT
and updated BaseTestClass
like this:
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class BaseTestClass {
@Autowired
WebApplicationContext context;
@BeforeEach
void setup(){
RestAssuredWebTestClient.standaloneSetup(context);
}
}
And again when I run mvn clean install
now it returns:
You haven't configured a WebTestClient instance. You can do this statically
RestAssuredWebTestClient.mockMvc(..)
RestAssuredWebTestClient.standaloneSetup(..);
RestAssuredWebTestClient.webAppContextSetup(..);
or using the DSL:
given().
mockMvc(..). ..
Btw I tried RestAssuredWebTestClient.standaloneSetup(new HelloWorldPortRESTAdapter());
as well in my BaseTestClass
but the result is same.
So how should I implement BaseTestClass
regarding EXPLICIT
and WEBTESTCLIENT
testModes?
Upvotes: 1
Views: 3013
Reputation: 928
I have struggled for 3 days, to makes RestAssuredWebTestClient works.
Thanks a llooottt for : https://www.baeldung.com/spring-5-webclient
That's how i could do that:
@WebFluxTest
public class AnimeControllerIntegrTest{
WebTestClient testClient;
@Test
public void get_RA() {
testClient = WebTestClient.bindToServer().baseUrl("http://localhost:8080/animes").build();
RestAssuredWebTestClient
.given()
.webTestClient(testClient)
.when()
.get()
.then()
.statusCode(OK.value())
.body("name" ,hasItem("paulo"))
;
}
}
Upvotes: 1
Reputation: 3912
Try passing an ApplicationContext
instance instead of WebApplicationContext
.
Upvotes: 0
Reputation: 11149
Please check the spring cloud contract samples https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/producer_webflux_webtestclient
And junit5
The
plugin
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>${spring-cloud-contract.version}</version>
<extensions>true</extensions>
<configuration>
<packageWithBaseClasses>com.example</packageWithBaseClasses>
<testMode>WEBTESTCLIENT</testMode>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<failIfNoTests>true</failIfNoTests>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit-platform-surefire-provider.version}</version>
</dependency>
</dependencies>
</plugin>
And The base class for junit5
public abstract class BeerRestBase {
@BeforeEach
public void setup() {
// remove::start[]
RestAssuredWebTestClient.standaloneSetup(new ProducerController(personToCheck -> personToCheck.age >= 20));
// remove::end[]
}
}
Upvotes: 0