Java Testing tools for 2024 - part 2

ivangavlik

ivan.gavlik

Posted on February 25, 2024

Java Testing tools for 2024 - part 2

TestContainers

Testcontainers is a library designed to help with integration testing by leveraging Docker containers. Docker containers provide lightweight, isolated environments that can be easily spun up and torn down, making them ideal for testing purposes. Testcontainers simplifies the process of managing docker containers within your tests, allowing you to seamlessly integrate them into your testing workflow.

In this example, Testcontainers automatically starts PostgreSQL and Redis containers before running the tests.


@Testcontainers
public class UserServiceIntegrationTest {

    @Container
    private static final PostgreSQLContainer<?> postgresContainer = new PostgreSQLContainer<>("postgres:latest");

    @Container
    private static final GenericContainer<?> redisContainer = new GenericContainer<>("redis:latest");

    @Autowired
    private UserService userService;

    @Test
    public void testUserCreation() {
        // Given
        User user = new User("John Doe", "john@example.com");

        // When
        userService.createUser(user);

        // Then
        User retrievedUser = userService.getUserByEmail("john@example.com");
        assertNotNull(retrievedUser);
        assertEquals("John Doe", retrievedUser.getName());
    }
}



Enter fullscreen mode Exit fullscreen mode

ArchUnit

Java library that allows developers to define and enforce architectural constraints within their codebase by writing unit tests. Lets see example:

We define an ArchUnit test to enforce a layered architecture. We specify three layers – Controller, Service, and Repository – and define rules governing their dependencies.


public class ArchitectureTest {

    private final JavaClasses classes = new ClassFileImporter().importPackages("com.example");

    @Test
    public void testLayeredArchitecture() {
        ArchRule layerDependencies = layeredArchitecture()
                .layer("Controller").definedBy("com.example.controller..")
                .layer("Service").definedBy("com.example.service..")
                .layer("Repository").definedBy("com.example.repository..")
                .whereLayer("Controller").mayNotBeAccessedByAnyLayer()
                .whereLayer("Service").mayOnlyBeAccessedByLayers("Controller")
                .whereLayer("Repository").mayOnlyBeAccessedByLayers("Service");

        layerDependencies.check(classes);
    }
}

Enter fullscreen mode Exit fullscreen mode

DataFaker

Library designed to simplify the generation of realistic test data for use in software testing, prototyping, and development environments.

Features:

  • Realistic Data Generation
  • Customization Options
  • Easy Integration

Example:


import com.github.javafaker.Faker;

public class UserGenerator {

    private final Faker faker;

    public UserGenerator() {
        this.faker = new Faker();
    }

    public User generateUser() {
        String firstName = faker.name().firstName();
        String lastName = faker.name().lastName();
        String email = faker.internet().emailAddress();
        String phoneNumber = faker.phoneNumber().cellPhone();
        String address = faker.address().fullAddress();

        return new User(firstName, lastName, email, phoneNumber, address);
    }
}

Enter fullscreen mode Exit fullscreen mode

Other mentions: Playwright - java framework for UI testing

💖 💪 🙅 🚩
ivangavlik
ivan.gavlik

Posted on February 25, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related