List of all spring properties:
Use “spring-boot-starter-test” to add all dependencies:
org.springframework.boot spring-boot-starter-test test
You can annotate the test class with :
To make rest call’s to the controllers, you can use one of the following:
@SpringBootTest will load your complete Spring application, but without the Servlet container when specifying (webEnvironment = MOCK).
When using webEnvironment = RANDOM_PORT, a random port is used (which you can find in the test using @LocalServerPort).
It is also possible to autowire a TestRestTemplate, RestAssuredMockMvc or MockMvc which you can use to test the application using relative url’s.
@MockBean can be used to mock any bean in the application environment.
To test the serialization and deserialization of a JSON object, you can use @JsonTest
To test a web client, you can use @RestClientTest
Examples of using the different annotations and rest clients can be found below:
Spring5 documentation about webFlux:
Spring Boot documentation about webFlux:
Spring testing documentation about Spring Web Reactive:
Spring5 documentation about webMVC:
Spring Boot documentation about webMVC:
Spring testing documentation about MVC Test Framework:
Links for using regex in java:
|Cheat sheet by zeroturnaround||:||http://files.zeroturnaround.com/pdf/zt_regular-expressions-cheat-sheet.pdf|
|Good tutorial by Vogella||:||http://www.vogella.com/tutorials/JavaRegularExpressions/article.html|
JUnit4 provides a standard dependency to the Hamcrest Assertion Frameworks which makes the following assertions possible:
Many matchers beside equalTo are available, but the problem to me is that I always have search for the appropriate one that I want to use, and static import that matcher.
An alternative to Hamcrest, is AssertJ which provides a fluent interface for the assertions, so you can use your IDE’s code completion for searching the matchers.
AssertJ uses the following syntax:
With AssertJ, it is also possible to use soft assertions which makes is possible to do multiple checks, and test all of them instead of stopping the test on the first failure.
SoftAssertions softly = new SoftAssertions(); softly.assertThat("Robert").isEqualTo("Robbert"); softly.assertThat("vd").isEqualTo("vd"); softly.assertThat("Zon").isEqualTo("Son"); softly.assertAll();
This results in the following error:
The following 2 assertions failed: 1) expected:<"Rob[b]ert"> but was:<"Robert"> at MyTest.test(MyTest.java:11) expected:<"Rob[b]ert"> but was:<"Robert"> 2) expected:<"[S]on"> but was:<"[Z]on"> at MyTest.test(MyTest.java:13) expected:<"[S]on"> but was:<"[Z]on">
More about AssertJ can be found here:http://joel-costigliola.github.io/assertj/
To add a dependency to AssertJ, use the following:
<dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>3.11.1</version> <scope>test</scope> </dependency>
In a previous blog I demonstrated how to create a java REST application using AWS Lambda and API Gateway.
In this blog we will modify the API Gateway so we can access the REST endpoint using a custom domain name and use https.
The endpoint can now be called on https://example.api.vdzon.com/