Test w/ JUnit 5 (Java)
0. Instroduction
This page is about JUnit 5.
For Mockito, see > https://sites.google.com/site/pawneecity/java-development/test-w-mockito-java
1. Reference
Tutorial
Testing Method Security
https://docs.spring.io/spring-security/site/docs/current/reference/html5/#test-method
Guide to JUnit 5 Parameterized Tests
https://www.baeldung.com/parameterized-tests-junit-5
2. IDE tools
EclEmma Java Code Coverage
It brings code coverage analysis directly into the Eclipse workbench.
Installation:
(Marketplace Client) https://marketplace.eclipse.org/content/eclemma-java-code-coverage
(Update Site) https://update.eclemma.org
Usage:
Verified w/ Spring Tool Suite 4.8 and 4.15
& w/ Eclipse 4.5
From the new icon "Coverage", execute:
Coverage As > JUnit Test
it will change to green color the code lines with coverage.
Nota: It applies over the selected item (a method, a class, all the project)
The new tab 'Coverage' indicates the coverage by packages and classes.
3. Migrating to JUnit 5
*JUnit 4* *JUnit 5*
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
@RunWith(SpringRunner.class) @ExtendWith(SpringExtension.class)
*Spring Boot*
@SpringBootTest(classes = { App.class }/* , webEnvironment = WebEnvironment.DEFINED_PORT */)
4. pom.xml (w/ Spring Security)
Tested with Spring Boot 2.4.3:
<!-- imports both Spring Boot test modules as well has JUnit, AssertJ, Hamcrest and other useful libraries -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- (at)WithMockUser, etc. -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
5. Samples
Import for JUnit 5 asserts:
import static org.junit.jupiter.api.Assertions.*;
assertThrows / assertDoesNotThrow
assertThrows sample:
@Test
void getAppkvNoRecordExpected() {
this.securityContextWithSuper();
UUID uuid = UUID.randomUUID();
Assertions.assertThrows(NotFoundAppException.class, () -> {
this.techController.getAppkv("Bearer jwt27", "en", uuid,
AppKV.KEY_DAILY_NIGHT_PROCESS_LAST_STACKTRACE);
});
}
assertDoesNotThrow sample:
assertDoesNotThrow( () -> this.myService.myMethod(myArgument) );
@WithMockUser(username, roles, authorities, password)
Note: See also @WithSecurityContext, @WithUserDetails, @WithAnonynousUser at
import org.springframework.security.test.context.support.WithMockUser;
@Test
@WithMockUser(authorities = { "ROLE_ADMIN" })
void getCalendartfShiftBbcRecordingsUrlsTest() {
//whatever
}
@ParameterizedTest / @ValueSource
@ParameterizedTest
@ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) // six numbers
void isOdd_ShouldReturnTrueForOddNumbers(int number) {
assertTrue(Numbers.isOdd(number));
}