Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Power of Data-Driven Testing

The Power of Data-Driven Testing

Elias Nogueira

October 18, 2023
Tweet

More Decks by Elias Nogueira

Other Decks in Technology

Transcript

  1. COPYRIGHT (C) 2023, ECLIPSE FOUNDATION . | THIS WORK IS

    LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2021-03 The Power of Data-Driven Testing A Deep Dive into Jakarta Persistence Specifications and NoSQL Elias Nogueira @eliasnogueira Otávio Santana @otaviojava
  2. What’s Jakarta NoSQL COPYRIGHT (C) 2023, ECLIPSE FOUNDATION | THIS

    WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2021-03
  3. COPYRIGHT (C) 2023, ECLIPSE FOUNDATION, INC. | THIS WORK IS

    LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2020-08 What’s Jakarta NoSQL JNoSQL is an compactible implementation of Jakarta NoSQL and Jakarta Data specification, a Java framework that streamlines the integration of Java applications with NoSQL databases.
  4. COPYRIGHT (C) 2023, ECLIPSE FOUNDATION, INC. | THIS WORK IS

    LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2020-08 What’s Jakarta Data Jakarta Data defines core APIs for the Jakarta EE platform allowing applications and other Jakarta EE components to explore the benefits of easy access to data technologies such as relational and non-relational databases, cloud-based data services, and so on. Jakarta Data provides an API to allow easy data access technologies. Thus, a Java developer can split the persistence and the model with several features such as a Repository interface with the method by query, where the framework will implement it.
  5. Early test status COPYRIGHT (C) 2023, ECLIPSE FOUNDATION | THIS

    WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2021-03
  6. COPYRIGHT (C) 2023, ECLIPSE FOUNDATION, INC. | THIS WORK IS

    LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2020-08 Early test status • Legacy code from different companies since 2017 • No defined development (testing included) practices • Several Proof of Concepts (PoC) around the projects
  7. Testing Modernization It’s the application of tools and techniques to

    increasing the reliability of your software. Steps applied to Jakarta NoSQL and Data COPYRIGHT (C) 2023, ECLIPSE FOUNDATION | THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2021-03 9 Testing Guidelines Moden Testing Library Data-Driven Approach Extensive Assertions Coverage Extension Containers for Testing
  8. A Testing Guideline is a group of testing best practices

    and approaches that must be followed by all the project contributors. Testing Guidelines A way to guide all the contributors towards quality COPYRIGHT (C) 2023, ECLIPSE FOUNDATION | THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2021-03
  9. COPYRIGHT (C) 2023, ECLIPSE FOUNDATION, INC. | THIS WORK IS

    LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2020-08 Testing Guidelines The Jakarta JNoSQL and Data has the following testing guidelines: • Tools to use (testing and related tools) • Naming conventions (test classes and methods, description and structure) • Specific test creation approach (soft assertions, extensive assertions) • Coverage extension (mutation testing)
  10. The adoption of JUnit 5 and it’s best features have

    decreased the code maintainability and increased coverage, making them more readable. Modern Testing Library The core part of the testing code COPYRIGHT (C) 2023, ECLIPSE FOUNDATION | THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2021-03
  11. COPYRIGHT (C) 2023, ECLIPSE FOUNDATION, INC. | THIS WORK IS

    LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2020-08 Modern Testing Library Features from JUnit 5 used in the project: • Test Description • Environment condition • Data Driven Testing ◦ Value Source ◦ Method Source ◦ Argument Source
  12. The usage of the Data-Driven Testing helps to easily increase

    the code coverage and decrease the code maintainability. Data-Driven Approach Iterate the same test code with different data COPYRIGHT (C) 2023, ECLIPSE FOUNDATION | THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2021-03
  13. COPYRIGHT (C) 2023, ECLIPSE FOUNDATION, INC. | THIS WORK IS

    LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2020-08 Data-Driven Approach Data-Driven Testing from JUnit 5 (JNoSQL, ValueWriterDecoratorTest) @ParameterizedTest(name = "must be compatible to {0}") @DisplayName("Should be able to verify the test compatibility") @ValueSource(classes = {Enum.class, Optional.class, Temporal.class}) void shouldVerifyCompatibility(Class<?> supportedClass) { assertThat(valueWriter.test(supportedClass)).isTrue(); }
  14. COPYRIGHT (C) 2023, ECLIPSE FOUNDATION, INC. | THIS WORK IS

    LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2020-08 Data-Driven Approach Data-Driven Testing from JUnit 5 (JNoSQL, TemporalWriterTest) @ParameterizedTest(name = "must convert {0}") @DisplayName("Should be able to convert temporal") @MethodSource("temporalDataForConversion") void shouldConvert(Temporal temporal) { String result = valueWriter.write(temporal); assertThat(result).isEqualTo(temporal.toString()); } static Stream<Arguments> temporalDataForConversion() { return Stream.of( arguments(LocalDateTime.now()), arguments(LocalDate.now()), arguments(LocalTime.now()), arguments(Year.now()), arguments(YearMonth.now()), arguments(ZonedDateTime.now()) ); }
  15. Libraries like AssertJ will add a range of different assertions,

    as well features to easy the validation like the soft assertion approach. Extensive Assertions Provide a way to extend the assertions COPYRIGHT (C) 2023, ECLIPSE FOUNDATION | THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2021-03
  16. COPYRIGHT (C) 2023, ECLIPSE FOUNDATION, INC. | THIS WORK IS

    LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2020-08 Extensive Assertions Soft Assertions (JNoSQL, KeyValueQueryParserTest) @Test @DisplayName("Should execute prepared statement using 'put' setting a value") void shouldExecutePrepareStatement2() { // code ignored assertSoftly(softly -> { softly.assertThat(entity.key()).as("key is equal").isEqualTo("Diana"); softly.assertThat(entity.value()).as("entity is equal").isEqualTo("Hunt"); softly.assertThat(duration).as("duration is equal").isEqualTo(ofSeconds(10L)); }); }
  17. COPYRIGHT (C) 2023, ECLIPSE FOUNDATION, INC. | THIS WORK IS

    LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2020-08 Extensive Assertions Specific Exceptions (JNoSQL, ConditionTest) @Test @DisplayName("Should throw IllegalArgumentException when condition is not found") void shouldThrowIllegalArgumentException() { String nonExistentCondition = "_NON_EXISTENT"; assertThatIllegalArgumentException() .isThrownBy(() -> Condition.parse(nonExistentCondition)) .withMessage(String.format("The condition %s is not found", nonExistentCondition)); }
  18. COPYRIGHT (C) 2023, ECLIPSE FOUNDATION, INC. | THIS WORK IS

    LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2020-08 Extensive Assertions Specific Exceptions (JNoSQL, DelQueryParserTest) @Test @DisplayName("Should throw QueryException when use parameter in query") void shouldReturnErrorWhenUseParameterInQuery() { assertThatThrownBy(() -> parser.query("del @id", manager)) .isInstanceOf(QueryException.class) .hasMessage("Use a PrepareStatement instead."); }
  19. PiTest will help you to catch issues undiscovered by your

    tests by mutating (changing them). Coverage Extension We can let tools mutate our code to find bugs COPYRIGHT (C) 2023, ECLIPSE FOUNDATION | THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2021-03
  20. COPYRIGHT (C) 2023, ECLIPSE FOUNDATION, INC. | THIS WORK IS

    LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2020-08 Coverage Extension PIT is a state of the art mutation testing system, providing gold standard test coverage for Java and the JVM. Mutation testing is conceptually quite simple. Faults (or mutations) are automatically seeded into your code, then your tests are run. If your tests fail then the mutation is killed, if your tests pass then the mutation lived.
  21. Testcontainers is a testing library that provides easy and lightweight

    APIs for bootstrapping integration tests with real services wrapped in Docker containers. Containers for Testing Don’t depend on manual steps anymore COPYRIGHT (C) 2023, ECLIPSE FOUNDATION | THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2021-03
  22. Takeaways • Guidelines are important to keep consistency • Modernization

    don’t take much time ◦ you can rely on tools like OpenRewrite to ease the changes • Proper tools help to be faster and assertive • Partially rely on tools for a better coverage COPYRIGHT (C) 2023, ECLIPSE FOUNDATION | THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2021-03
  23. COPYRIGHT (C) 2023, ECLIPSE FOUNDATION, INC. | THIS WORK IS

    LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2020-08 References • JNoSQL Project: https://github.com/eclipse/jnosql • Jakarta Data Project: https://github.com/jakartaee/data • JUnit 5: https://junit.org/junit5/ • AssertJ: https://github.com/assertj/assertj • PIT: https://pitest.org/ • Testcontainers: https://testcontainers.com/
  24. THANK YOU! COPYRIGHT (C) 2023, ECLIPSE FOUNDATION | THIS WORK

    IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE (CC BY 4.0) | V2021-03 Elias Nogueira @eliasnogueira Otávio Santana @otaviojava