Skip to content

Commit

Permalink
add junit config to auto register extension
Browse files Browse the repository at this point in the history
  • Loading branch information
paulheinr committed Dec 13, 2023
1 parent ae32b59 commit d949ed2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
40 changes: 12 additions & 28 deletions matsim/src/test/java/org/matsim/api/core/v01/AutoResetIdCaches.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,31 @@

package org.matsim.api.core.v01;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;

/**
* Auto-resets Id caches before each test is started. This helps to keep every single unit test independent of others
* (so things like execution order etc. will not impact them).
* <p>
* It is enabled in tests (run by maven) by registering them as listeners in the surefire and failsafe plugin configs:
* <pre>
* {@code
* <property>
* <name>listener</name>
* <value>org.matsim.api.core.v01.IdCacheCleaner</value>
* </property>
* }
* </pre>
* It is configured in the junit-platform.properties file and in META-INF/services/org.junit.jupiter.api.extension.Extension file.
* (Also see https://www.baeldung.com/junit-5-extensions)
* Both files are located in the root of the matsim project in order to inherit the auto extension in all submodules.
* <p>
* IntelliJ does not support junit listeners out of the box. To enable them in IntelliJ, you can install a plugin
* https://plugins.jetbrains.com/plugin/15718-junit-4-surefire-listener
* IntelliJ does also recognize this configuration and will automatically enable the extension.
* <p>
* Not sure about Eclipse, but it seems that an additional plugin would be required (see:
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=538885)
* <p>
* Since IDEs have a limited support for registering jUnit RunListeners via pom.xml, there may be cases where we need
* to explicitly reset the Id caches while setting up a test (to make them green in IDEs).
* For some reason, the configuration files need to be placed in the matsim module (where this class is placed). Otherwise, it won't work.
*
* @author Michal Maciejewski (michalm)
*/
public class AutoResetIdCaches implements TestWatcher {
@Override
public void testSuccessful(ExtensionContext context) {
Id.resetCaches();
}

@Override
public void testAborted(ExtensionContext context, Throwable cause) {
Id.resetCaches();
}
public class AutoResetIdCaches implements TestInstancePostProcessor {
private static final Logger log = LogManager.getLogger(AutoResetIdCaches.class);

@Override
public void testFailed(ExtensionContext context, Throwable cause) {
public void postProcessTestInstance(Object o, ExtensionContext extensionContext) throws Exception {
log.info("Resetting Id caches.");
Id.resetCaches();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.matsim.api.core.v01.AutoResetIdCaches
1 change: 1 addition & 0 deletions matsim/src/test/resources/junit-platform.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
junit.jupiter.extensions.autodetection.enabled=true
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,20 @@
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.pdf</exclude>
<exclude>**/*.gz</exclude>
</excludes>
<includes>
<include>org.junit.api.extension.Extension</include>
<include>junit-platform.properties</include>
</includes>
</resource>
</resources>
</build>

<profiles>
Expand Down

0 comments on commit d949ed2

Please sign in to comment.