Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

ivan.gavlik
ivan.gavlik

Posted on

     

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. Testcontainerssimplifies 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.

@TestcontainerspublicclassUserServiceIntegrationTest{@ContainerprivatestaticfinalPostgreSQLContainer<?>postgresContainer=newPostgreSQLContainer<>("postgres:latest");@ContainerprivatestaticfinalGenericContainer<?>redisContainer=newGenericContainer<>("redis:latest");@AutowiredprivateUserServiceuserService;@TestpublicvoidtestUserCreation(){// GivenUseruser=newUser("John Doe","john@example.com");// WhenuserService.createUser(user);// ThenUserretrievedUser=userService.getUserByEmail("john@example.com");assertNotNull(retrievedUser);assertEquals("John Doe",retrievedUser.getName());}}
Enter fullscreen modeExit 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.

publicclassArchitectureTest{privatefinalJavaClassesclasses=newClassFileImporter().importPackages("com.example");@TestpublicvoidtestLayeredArchitecture(){ArchRulelayerDependencies=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 modeExit 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:

importcom.github.javafaker.Faker;publicclassUserGenerator{privatefinalFakerfaker;publicUserGenerator(){this.faker=newFaker();}publicUsergenerateUser(){StringfirstName=faker.name().firstName();StringlastName=faker.name().lastName();Stringemail=faker.internet().emailAddress();StringphoneNumber=faker.phoneNumber().cellPhone();Stringaddress=faker.address().fullAddress();returnnewUser(firstName,lastName,email,phoneNumber,address);}}
Enter fullscreen modeExit fullscreen mode

Other mentions: Playwright - java framework for UI testing

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
vivienne-m profile image
Vivienne Medrano
  • Joined

Great overview of Java testing tools for 2024! ArchUnit seems like a powerful tool for enforcing architectural constraints through unit tests, and the example showcasing layered architecture is really informative. Thanks for sharing these valuable resources!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I've been in programing for 8 years, and I'm still surprised at how much of it is just me talking to my computer.
  • Joined

More fromivan.gavlik

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp