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.
Join the DZone community and get the full member experience.
Join For FreeThe 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:
<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.
@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.
@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
Opinions expressed by DZone contributors are their own.
Comments