DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • I Don’t TDD: Pragmatic Testing With Java
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers
  • Top 7 Mistakes When Testing JavaFX Applications

Trending

  • Migrate a Hardcoded LangGraph Agent to LaunchDarkly AI Configs in 20 Minutes
  • Why DDoS Protection Is an Architectural Decision for Developers
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • From Indicators to Insights: Automating IOC Enrichment Using Python and Threat Feeds
  1. DZone
  2. Coding
  3. Java
  4. Creating Your Swiss Army Knife on Java Test Stack

Creating Your Swiss Army Knife on Java Test Stack

What are the excellent libraries for a Java project? A good project must have tests to increase quality. In this video, learn about those three libraries and how to take advantage of them.

By 
Otavio Santana user avatar
Otavio Santana
DZone Core CORE ·
Feb. 25, 23 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
7.3K Views

Join the DZone community and get the full member experience.

Join For Free

The code testabilities guarantee several excellent points in an efficient code design, such as maintainability; it helps with the documentation and makes it easier to refactor and build an evolutionary design/architecture. There is no doubt about it, but what is a good test stack to start a project? This video will explain the minimum test stack to start your project with Java.

The first point to understand when we talk about the minimum is that there are no silver bullets on the test stack. Eventually, we need to include or remove dependencies, especially when we talk about legacy code. Given that, I'll give what my favorite test stack on Java is, and I use it and recommend as a minimum for starting from scratch:

  • JUnit-Jupiter: The fifth version of JUnit breaks compatibility with the previous version; however, it is worth it because it brings several features, mainly to create custom extensions.

  • Mockito: A super popular mock framework with Java. This popularity and huge community enable several extensions, such as with JUnit-Jupiter.

  • AssertJ: If you love fluent API like me, thus, AssertJ is an outlandish library for you.  

Using the maven project, you can add those libraries' dependencies:

XML
 
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>${mockito.verson}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-junit-jupiter</artifactId>
    <version>${mockito.verson}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>${assertj.verson}</version>
    <scope>test</scope>
</dependency>


Given a soccer championship league context, where we have a team that should have eleven players, each name must be unique in the championship league. We can explore tests on it.

Starting with JMockito and test, we need to test the service where we need to check if the name does exist from the database to procedure the logic. At this point, we don't need it and don't want to create an integration test. 

We want to focus on the business, so we don't want to test the database or framework but only my code. So, we'll mock the repository with two scenarios, when there is the name and when there is no.

Using Mockito integration with JUnit-Jupiter you can inject mock only using annotations. The code below shows the test using JMockito, where we don't need to have a database or get out of the focus to test the business.

Java
 
@ExtendWith(MockitoExtension.class)
class ChampionshipServiceTest {

    @Mock
    private ChampionRepository repository;

    @InjectMocks
    private ChampionshipService service;

    @Test
    public void shouldRegister() {
        Mockito.when(repository.existByName("Bahia")).thenReturn(false);
        Team bahia = Team.of("Bahia");
        service.register(bahia);

        Mockito.verify(repository).existByName("Bahia");
    }
    @Test
    @DisplayName("should return an exception when there is a team with a name")
    public void shouldThrownAnExceptionWhenTeamNameExists() {
        Mockito.when(repository.existByName("Bahia")).thenReturn(true);
        Team bahia = Team.of("Bahia");
        Assertions.assertThrows(IllegalArgumentException.class, () ->
                service.register(bahia));

        Mockito.verify(repository).existByName("Bahia");
    }
}


AssertJ makes the assertions easier to understand, thanks to the fluent API that the library supports. You can test collections, maps, or a single instance. Such as, we can verify adding a player to the team using it.

Java
 
@Test
public void shouldCreatePlayers() {
    Team bahia = Team.of("Bahia");
    bahia.add(Player.of("Neymar", "Santos", 10));
    bahia.add(Player.of("Cristiano Ronaldo", "Lisbon", 10));

    org.assertj.core.api.Assertions.assertThat(bahia.players())
            .hasSize(2)
            .map(Player::name)
            .contains("Neymar", "Cristiano Ronaldo");
}


Take this and more information on this video.

Takeaways:

  • A minimum test stack to start Java

  • Learn more about JUnit and why it is a good choice

  • Learn about mocks exploring with JMockito

  • Make your test beautiful with the fluent-API Assert: AssertJ



JUnit Mockito Java (programming language) Testing

Opinions expressed by DZone contributors are their own.

Related

  • I Don’t TDD: Pragmatic Testing With Java
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers
  • Top 7 Mistakes When Testing JavaFX Applications

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook