I run the tests in parallel. And after running all the tests, the analysis check should start.
I can't understand why there is no execution of the general method after all the nodes of the test class are executed.
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.springframework.core.io.ClassPathResource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import static org.assertj.core.api.Assertions.assertThat;
@Execution(ExecutionMode.CONCURRENT)
public class FirstTest {
private static List<String> threadNames = Collections.synchronizedList(new ArrayList<>());
@AfterAll
public static void afterAllTest() {
Assumptions.assumeTrue(FirstTest::isParallelExecutionEnable);
long count = threadNames.stream()
.distinct()
.count();
System.out.println();
System.out.println("count : " + count);
assertThat(count).isEqualTo(2);
}
private static boolean isParallelExecutionEnable() {
String ENABLED_PARALLEL_EXECUTION = "junit.jupiter.execution.parallel.enabled";
String PARALLELISM_FACTOR = "junit.jupiter.execution.parallel.config.fixed.parallelism";
try {
Properties junitConfig = new Properties();
junitConfig.load(new ClassPathResource("junit-platform.properties").getInputStream());
boolean enabled =
Boolean.valueOf(junitConfig.getProperty(ENABLED_PARALLEL_EXECUTION, "false"));
if (!enabled) return false;
int parallelismFactor =
Integer.valueOf(junitConfig.getProperty(PARALLELISM_FACTOR, "1"));
int moreThanOneThread = 1;
return parallelismFactor > moreThanOneThread;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Test
void helloTest() throws InterruptedException {
Thread.sleep(1000);
String name = Thread.currentThread().getName();
threadNames.add(name);
System.out.println("Hello! " + name);
}
@Test
void worldTest() throws InterruptedException {
Thread.sleep(1000);
String name = Thread.currentThread().getName();
threadNames.add(name);
System.out.println("World! " + name);
}
}
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.javabydeveloper</groupId>
<artifactId>Junit5-parallel-tests-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<!-- Dependency versions -->
<junit.jupiter.version>5.5.2</junit.jupiter.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<!-- Encoding -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I get no errors and the tests go well, but the runtime analysis doesn't work out.
Can someone explain why the last method marked with the @AfterAll annotation does not work ?