Java language is one of the most successful languages nowadays with more than 20 years of history. This means that a lot of tools libraries has been developed around all this time for production code but also for testing code.
Come to this session to learn 10 testing libraries that can have a really positive impact on how to write tests, but more importantly, also how to make them more readable.
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
35. @alexsotob35
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
42. @alexsotob42
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
59. @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();
}
59
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
60. @alexsotob
APE SQL example
@After
public void clean_database() {
// Given:
rdbmsPopulator.forUri(jdbcUri).withDriver(Driver.class).withUsername("sa")
.withPassword("").usingDataSet("composers.yml")
.clean();
}
60
Clean After Test
Clean Database
68. @alexsotob
Arquillian Graphene Page Object example
@Location("login.xhtml")
public class HomePage {
@Page
private UserPage userPage;
@FindBy(id = "submit")
private WebElement submitButton;
@FindBy(id = "form")
private LoginPopup loginPopup;
// methods
}
68
Location of the page
Another Page Object
Find web element by id
Page fragment
77. @alexsotob77
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