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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • I Don’t TDD: Pragmatic Testing With Java
  • Spring 5 Web Reactive: Flux, Mono, and JUnit Testing
  • Why Testing is a Long-Term Investment for Software Engineers
  • Selenium Grid Tutorial: Essential Tips and How to Set It Up

Trending

  • Simplifying Multi-LLM Integration With KubeMQ
  • Integrating Model Context Protocol (MCP) With Microsoft Copilot Studio AI Agents
  • How to Ensure Cross-Time Zone Data Integrity and Consistency in Global Data Pipelines
  • Simpler Data Transfer Objects With Java Records
  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
6.8K 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
  • Spring 5 Web Reactive: Flux, Mono, and JUnit Testing
  • Why Testing is a Long-Term Investment for Software Engineers
  • Selenium Grid Tutorial: Essential Tips and How to Set It Up

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!