One of the most important benefits of automated testing is to ensure a fast and safe code refactoring to evolve our system architecture. The main problem is how to write tests that are easy to write, easy to follow and not time consuming during development nor execution time. In this session, we are going to explore some powerful Java testing libraries that will help you write better (unit) tests focusing on the main Unicorns architecture challenges such as validating microservices endpoints, remote calls to other microservices or just asynchronous/reactive code.
16. @alexsotob16
Benefits of AssertJ
> IDE Friendly
Ctrl + Space works
> Assertions Generation
ComposerAssert.assertThat(mozart).hasName(“Mozart”).hasBirthdate(LocalDate.of(..);
> Out-of-the-Box Assertions
Guava, Joda, DB, Neo4j and Swing
28. @alexsotob
REST-assured Example
@Test
public void should_find_composer() {
given()
.when()
.get("{composer}", "Ludwig van Beethoven")
.then()
.assertThat()
.body("name", is("Ludwig van Beethoven"))
.body("operas.size()", is(1))
.body("operas.name", hasItems("Fidelio"));
}
28
GET Http Method with
Placeholders
GPath Expression
29. @alexsotob
REST-assured Example
@Test
public void should_insert_composer() {
Composer composer = ....;
given()
.param("parameter1", "parameterValue")
.body(composer)
.when()
.post()
.then()
.assertThat()
.statusCode(201);
}
29
Create POJO object
Set Params
Set POJO as Body
POST Http Method
Assertion on Status Code
30. @alexsotob
REST-assured Request Logging
given().log().all(). .. // Log all request specification details
given().log().params(). .. // Log only the parameters of the request
given().log().body(). .. // Log only the request body
given().log().headers(). .. // Log only the request headers
given().log().cookies(). .. // Log only the request cookies
given().log().method(). .. // Log only the request method
given().log().path(). .. // Log only the request path
30
34. @alexsotob34
More Features of Rest-Assured
> Custom Parsers
Not just JSON and XML
> SSL Support
.relaxedHTTPSValidation()
> Filters
Input/Output modification
> JSON Schema Validation
Content not Important
41. @alexsotob41
Benefits of Service Virtualization and Hoverfly
> Do not relay on network
Yet testing all stack trace
> Hoverfly multilanguage
Can be used as standalone proxy
> Hoverfly JVM
Integration with JVM proxy settings
58. @alexsotob
APE SQL example
@Rule
public ArquillianPersistenceRule arquillianPersistenceRule =
new ArquillianPersistenceRule();
@DbUnit
@ArquillianResource
RdbmsPopulator rdbmsPopulator;
@Before
public void populateData() {
// Given:
rdbmsPopulator.forUri(jdbcUri).withDriver(Driver.class).withUsername("sa")
.withPassword("").usingDataSet("composers.yml")
.execute();
}
58
APE JUnit Rule
(not necessary with
Arquillian Runner)
Set DBUnit usage
Configure Connection and Dataset
Populate
composers:
- id: 1
name: Wolfgang Amadeus Mozart
birthdate: 27/1/1756
died: 5/12/1791
59. @alexsotob
APE SQL example
@After
public void clean_database() {
// Given:
rdbmsPopulator.forUri(jdbcUri).withDriver(Driver.class).withUsername("sa")
.withPassword("").usingDataSet("composers.yml")
.clean();
}
59
Clean After Test
Clean Database
66. @alexsotob66
More about Smart Testing
> Heuristics
new, changed, affected, failed and categorized
> Range of Commits
Define where you want to look at
> Configuration
System Properties or YAML file (global or per module)
> Jenkins Pipeline Shared Library
Integrate with your CI/CD pipeline
> Detection of none class changes
@WatchFile("src/main/resources/META-INF/
> Surefire based
Works with any kind of test that can be run in Surefire
72. @alexsotob72
More about Diferencia
> Comparison
with, without noise detection, strict, subset
> Formats supported
JSON, Plain Text (automatic detection), HTTPS
> Monitoring
HTTP Status, Prometheus
> Rest API
Configure, Stats
> Docker Image
Ready to use in Kubernetes and OpenShift