Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to JUnit 5 #2771

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c0e9f26
Migrate tests to JUnit5 (work in progress)
ljacqu Apr 26, 2019
f96f838
Fix some of the test failures
ljacqu Apr 27, 2019
2e0f0cc
Fix ConsoleLoggerTest breaking subsequent tests
ljacqu Apr 27, 2019
e9f7d95
Revert to TemporaryFolder for test using Junit-vintage runner
ljacqu Apr 27, 2019
16ef0ce
Migrate parameterized tests to JUnit5
ljacqu Apr 27, 2019
cdec468
Migrate DelayedInjectionRunner to JUnit 5 extension (work in progress)
ljacqu May 1, 2019
5f5fb0c
Merge remote-tracking branch 'origin/master' into junit5-migration
ljacqu May 2, 2019
82200d4
Merge remote-tracking branch 'origin/master' into junit5-migration
ljacqu Oct 8, 2019
1873a21
Junit5 migration: fix failing test & migrate new tests
ljacqu Oct 8, 2019
8060b11
Merge remote-tracking branch 'origin/master' into junit5-migration
ljacqu Nov 6, 2019
85a2f13
Change Junit5 tests to package-private visibility
ljacqu Nov 7, 2019
74a104e
Use 'assertThat' from Hamcrest instead of Junit4
ljacqu Nov 7, 2019
31dcb38
Merge branch 'master' of https://github.com/AuthMe/AuthMeReloaded int…
ljacqu Mar 30, 2020
ebd8ff8
Merge remote-tracking branch 'origin/junit5-migration' into junit5-mi…
ljacqu Mar 30, 2020
d12f304
update
ljacqu May 2, 2020
210f867
Merge remote-tracking branch 'origin/master' into junit5-migration
ljacqu Apr 19, 2021
4a3ee42
Migrate BukkitServiceTest and CommandManagerTest to JUnit 5
ljacqu Apr 19, 2021
cbfd884
Migrate last tests to JUnit 5
ljacqu Apr 19, 2021
17d5691
Replace assumption/assertTrue imports from Junit4 and remove JUnit vi…
ljacqu Apr 19, 2021
68f1322
Merge remote-tracking branch 'origin/master' into junit5-migration
ljacqu Jan 30, 2022
d3a5501
Merge remote-tracking branch 'origin/master' into junit5-migration
ljacqu Aug 23, 2023
ee42437
Merge branch 'master' into junit5-migration
ljacqu Dec 20, 2023
67ec218
Update test dependencies / remove unnecessary public modifiers
ljacqu Dec 20, 2023
13087f7
Replace "InjectDelayed" extension everywhere with Mockito extension
ljacqu Dec 20, 2023
6161d95
Keep Mockito versions in sync; small fixes after JUnit 5 migration
ljacqu Dec 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<version>0.8.11</version>
<executions>
<execution>
<id>pre-unit-test</id>
Expand Down Expand Up @@ -1010,30 +1010,36 @@
<!-- Unit Testing Libraries -->

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
<version>4.13.2</version>
<version>5.10.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
<version>5.10.1</version>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
<version>2.0.0.0</version>
<version>2.2</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
<version>4.8.1</version>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
<version>4.11.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
<version>4.11.0</version>
</dependency>

<!-- Required to mock the LuckPerms API-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class HelpMessagesService {
private final HelpMessagesFileHandler helpMessagesFileHandler;

@Inject
HelpMessagesService(HelpMessagesFileHandler helpMessagesFileHandler) {
public HelpMessagesService(HelpMessagesFileHandler helpMessagesFileHandler) {
this.helpMessagesFileHandler = helpMessagesFileHandler;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.bukkit.configuration.file.YamlConfiguration;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.File;

import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_LANGUAGE;
Expand All @@ -24,18 +23,16 @@ public abstract class AbstractMessageFileHandler implements Reloadable {

private final ConsoleLogger logger = ConsoleLoggerFactory.get(AbstractMessageFileHandler.class);

@DataFolder
@Inject
private File dataFolder;

@Inject
private Settings settings;
private final File dataFolder;
private final Settings settings;

private String filename;
private FileConfiguration configuration;
private final String defaultFile;

protected AbstractMessageFileHandler() {
protected AbstractMessageFileHandler(@DataFolder File dataFolder, Settings settings) {
this.dataFolder = dataFolder;
this.settings = settings;
this.defaultFile = createFilePath(DEFAULT_LANGUAGE);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package fr.xephi.authme.message;

import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.FileUtils;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import javax.inject.Inject;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

Expand All @@ -21,8 +24,9 @@ public class HelpMessagesFileHandler extends AbstractMessageFileHandler {

private FileConfiguration defaultConfiguration;

@Inject // Trigger injection in the superclass
HelpMessagesFileHandler() {
@Inject
public HelpMessagesFileHandler(@DataFolder File dataFolder, Settings settings) {
super(dataFolder, settings);
}

/**
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/fr/xephi/authme/message/MessagesFileHandler.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package fr.xephi.authme.message;

import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.message.updater.MessageUpdater;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.settings.Settings;

import javax.inject.Inject;
import java.io.File;

import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_LANGUAGE;

Expand All @@ -15,10 +18,12 @@ public class MessagesFileHandler extends AbstractMessageFileHandler {

private final ConsoleLogger logger = ConsoleLoggerFactory.get(MessagesFileHandler.class);

@Inject
private MessageUpdater messageUpdater;
private final MessageUpdater messageUpdater;

MessagesFileHandler() {
@Inject
MessagesFileHandler(@DataFolder File dataFolder, Settings settings, MessageUpdater messageUpdater) {
super(dataFolder, settings);
this.messageUpdater = messageUpdater;
}

@Override
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/fr/xephi/authme/service/HelpTranslationGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@
*/
public class HelpTranslationGenerator {

@Inject
private CommandInitializer commandInitializer;

@Inject
private HelpMessagesService helpMessagesService;
private final CommandInitializer commandInitializer;
private final HelpMessagesService helpMessagesService;
private final Settings settings;
private final File dataFolder;

@Inject
private Settings settings;

@DataFolder
@Inject
private File dataFolder;
HelpTranslationGenerator(CommandInitializer commandInitializer, HelpMessagesService helpMessagesService,
Settings settings, @DataFolder File dataFolder) {
this.commandInitializer = commandInitializer;
this.helpMessagesService = helpMessagesService;
this.settings = settings;
this.dataFolder = dataFolder;
}

/**
* Updates the help file to contain entries for all commands.
Expand Down
38 changes: 16 additions & 22 deletions src/test/java/fr/xephi/authme/AuthMeInitializationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,22 @@
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.logging.Logger;

import static fr.xephi.authme.settings.properties.AuthMeSettingsRetriever.buildConfigurationData;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
Expand All @@ -52,32 +51,25 @@
* Integration test verifying that all services can be initialized in {@link AuthMe}
* with the {@link Injector}.
*/
@RunWith(MockitoJUnitRunner.class)
public class AuthMeInitializationTest {
@ExtendWith(MockitoExtension.class)
class AuthMeInitializationTest {

@Mock
private Server server;

@Mock
private PluginManager pluginManager;

private AuthMe authMe;
private File dataFolder;

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@TempDir
File dataFolder;

@Before
public void initAuthMe() throws IOException {
dataFolder = temporaryFolder.newFolder();
@BeforeEach
void initAuthMe() throws IOException {
File settingsFile = new File(dataFolder, "config.yml");
given(server.getLogger()).willReturn(Logger.getAnonymousLogger());
JavaPluginLoader pluginLoader = new JavaPluginLoader(server);
Files.copy(TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "config.test.yml"), settingsFile);

// Mock / wire various Bukkit components
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
given(server.getPluginManager()).willReturn(pluginManager);

// PluginDescriptionFile is final: need to create a sample one
PluginDescriptionFile descriptionFile = new PluginDescriptionFile(
Expand All @@ -88,13 +80,15 @@ public void initAuthMe() throws IOException {
}

@Test
public void shouldInitializeAllServices() {
void shouldInitializeAllServices() {
// given
PropertyReader reader = mock(PropertyReader.class);
PropertyResource resource = mock(PropertyResource.class);
given(resource.createReader()).willReturn(reader);
Settings settings = new Settings(dataFolder, resource, null, buildConfigurationData());

PluginManager pluginManager = mock(PluginManager.class);
given(server.getPluginManager()).willReturn(pluginManager);
TestHelper.setupLogger();

Injector injector = new InjectorBuilder()
Expand Down Expand Up @@ -128,7 +122,7 @@ public void shouldInitializeAllServices() {
}

@Test
public void shouldHandlePrematureShutdownGracefully() {
void shouldHandlePrematureShutdownGracefully() {
// given
BukkitScheduler scheduler = mock(BukkitScheduler.class);
given(server.getScheduler()).willReturn(scheduler);
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/fr/xephi/authme/ClassesConsistencyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import fr.xephi.authme.util.expiring.ExpiringMap;
import fr.xephi.authme.util.expiring.ExpiringSet;
import fr.xephi.authme.util.expiring.TimedCounter;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.lang.reflect.Field;
Expand All @@ -32,14 +32,14 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Contains consistency tests across all AuthMe classes.
*/
public class ClassesConsistencyTest {
class ClassesConsistencyTest {

/** Contains all production classes. */
private static final List<Class<?>> ALL_CLASSES =
Expand Down Expand Up @@ -71,7 +71,7 @@ int.class, long.class, float.class, String.class, File.class, Enum.class, collec
* Checks that there aren't two classes with the same name; this is confusing and should be avoided.
*/
@Test
public void shouldNotHaveSameName() {
void shouldNotHaveSameName() {
// given
Set<String> names = new HashSet<>();

Expand All @@ -87,7 +87,7 @@ public void shouldNotHaveSameName() {
* Checks that fields of classes are either private or static final fields of an immutable type.
*/
@Test
public void shouldHaveNonPrivateConstantsOnly() {
void shouldHaveNonPrivateConstantsOnly() {
// given / when
Set<String> invalidFields = ALL_CLASSES.stream()
.filter(clz -> !CLASSES_EXCLUDED_FROM_VISIBILITY_TEST.contains(clz))
Expand Down Expand Up @@ -145,7 +145,7 @@ private static String formatField(Field field) {
* interface to regularly clean up expired data.
*/
@Test
public void shouldImplementHasCleanup() {
void shouldImplementHasCleanup() {
// given / when / then
for (Class<?> clazz : ALL_CLASSES) {
if (hasExpiringCollectionAsField(clazz) && !EXPIRING_STRUCTURES.contains(clazz)) {
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/fr/xephi/authme/CodeClimateConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Consistency test for the CodeClimate configuration file.
*/
public class CodeClimateConfigTest {
class CodeClimateConfigTest {

private static final String CONFIG_FILE = ".codeclimate.yml";

@Test
public void shouldHaveExistingClassesInExclusions() {
void shouldHaveExistingClassesInExclusions() {
// given / when
FileConfiguration configuration = YamlConfiguration.loadConfiguration(new File(CONFIG_FILE));
List<String> excludePaths = configuration.getStringList("exclude_patterns");
Expand Down
Loading