From a625e699a8726dc733169af86c5023405697bcb9 Mon Sep 17 00:00:00 2001 From: Sally MacFarlane Date: Fri, 15 Dec 2023 07:43:10 +1000 Subject: [PATCH] [MINOR] More cli tests to junit 5 (#6283) * more CLI and services tests to junit5 --------- Signed-off-by: Sally MacFarlane Co-authored-by: Fabio Di Fabio Signed-off-by: jflo --- .../besu/cli/CommandLineUtilsTest.java | 8 ++-- ...nvironmentVariableDefaultProviderTest.java | 8 ++-- .../TomlConfigFileDefaultProviderTest.java | 43 +++++++++---------- .../ConfigOptionSearchAndRunHandlerTest.java | 42 ++++++++++-------- .../besu/services/BesuEventsImplTest.java | 33 ++++++++------ .../besu/services/PicoCLIOptionsImplTest.java | 12 +++--- 6 files changed, 78 insertions(+), 68 deletions(-) diff --git a/besu/src/test/java/org/hyperledger/besu/cli/CommandLineUtilsTest.java b/besu/src/test/java/org/hyperledger/besu/cli/CommandLineUtilsTest.java index f8bc8d8972d..726bf38965e 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/CommandLineUtilsTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/CommandLineUtilsTest.java @@ -35,18 +35,18 @@ import java.util.List; import java.util.Map; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import picocli.CommandLine; import picocli.CommandLine.Command; import picocli.CommandLine.Option; import picocli.CommandLine.RunLast; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class CommandLineUtilsTest { @SuppressWarnings("PrivateStaticFinalLoggers") // @Mocks are inited by JUnit @Mock diff --git a/besu/src/test/java/org/hyperledger/besu/cli/EnvironmentVariableDefaultProviderTest.java b/besu/src/test/java/org/hyperledger/besu/cli/EnvironmentVariableDefaultProviderTest.java index af609327340..9953ca519f7 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/EnvironmentVariableDefaultProviderTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/EnvironmentVariableDefaultProviderTest.java @@ -21,13 +21,13 @@ import java.util.HashMap; import java.util.Map; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; import picocli.CommandLine.Model.OptionSpec; import picocli.CommandLine.Model.PositionalParamSpec; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class EnvironmentVariableDefaultProviderTest { private final Map environment = new HashMap<>(); diff --git a/besu/src/test/java/org/hyperledger/besu/cli/TomlConfigFileDefaultProviderTest.java b/besu/src/test/java/org/hyperledger/besu/cli/TomlConfigFileDefaultProviderTest.java index 1481db9526d..5185a4133e3 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/TomlConfigFileDefaultProviderTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/TomlConfigFileDefaultProviderTest.java @@ -26,31 +26,29 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; +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 picocli.CommandLine; import picocli.CommandLine.Model.CommandSpec; import picocli.CommandLine.Model.OptionSpec; import picocli.CommandLine.ParameterException; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class TomlConfigFileDefaultProviderTest { @Mock CommandLine mockCommandLine; @Mock CommandSpec mockCommandSpec; - @Rule public final TemporaryFolder temp = new TemporaryFolder(); - @Test - public void defaultValueForMatchingKey() throws IOException { + public void defaultValueForMatchingKey(final @TempDir Path temp) throws IOException { when(mockCommandLine.getCommandSpec()).thenReturn(mockCommandSpec); Map validOptionsMap = new HashMap<>(); validOptionsMap.put("--a-short-option", null); @@ -58,7 +56,7 @@ public void defaultValueForMatchingKey() throws IOException { validOptionsMap.put("--a-longer-option", null); when(mockCommandSpec.optionsMap()).thenReturn(validOptionsMap); - final File tempConfigFile = temp.newFile("config.toml"); + final File tempConfigFile = temp.resolve("config.toml").toFile(); try (final BufferedWriter fileWriter = Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8)) { @@ -106,7 +104,7 @@ public void defaultValueForMatchingKey() throws IOException { } @Test - public void defaultValueForOptionMustMatchType() throws IOException { + public void defaultValueForOptionMustMatchType(final @TempDir Path temp) throws IOException { when(mockCommandLine.getCommandSpec()).thenReturn(mockCommandSpec); Map validOptionsMap = new HashMap<>(); validOptionsMap.put("--a-boolean-option", null); @@ -124,7 +122,7 @@ public void defaultValueForOptionMustMatchType() throws IOException { when(mockCommandSpec.optionsMap()).thenReturn(validOptionsMap); - final File tempConfigFile = temp.newFile("config.toml"); + final File tempConfigFile = temp.resolve("config.toml").toFile(); try (final BufferedWriter fileWriter = Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8)) { @@ -238,9 +236,9 @@ public void configFileNotFoundMustThrow() { } @Test - public void invalidConfigMustThrow() throws IOException { + public void invalidConfigMustThrow(final @TempDir Path temp) throws IOException { - final File tempConfigFile = temp.newFile("config.toml"); + final File tempConfigFile = Files.createTempFile("invalid", "toml").toFile(); final TomlConfigFileDefaultProvider providerUnderTest = new TomlConfigFileDefaultProvider(mockCommandLine, tempConfigFile); @@ -254,9 +252,9 @@ public void invalidConfigMustThrow() throws IOException { } @Test - public void invalidConfigContentMustThrow() throws IOException { + public void invalidConfigContentMustThrow(final @TempDir Path temp) throws IOException { - final File tempConfigFile = temp.newFile("config.toml"); + final File tempConfigFile = temp.resolve("config.toml").toFile(); final BufferedWriter fileWriter = Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8); fileWriter.write("an-invalid-syntax=======...."); @@ -276,13 +274,13 @@ public void invalidConfigContentMustThrow() throws IOException { } @Test - public void unknownOptionMustThrow() throws IOException { + public void unknownOptionMustThrow(final @TempDir Path temp) throws IOException { when(mockCommandLine.getCommandSpec()).thenReturn(mockCommandSpec); Map validOptionsMap = new HashMap<>(); when(mockCommandSpec.optionsMap()).thenReturn(validOptionsMap); - final File tempConfigFile = temp.newFile("config.toml"); + final File tempConfigFile = temp.resolve("config.toml").toFile(); final BufferedWriter fileWriter = Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8); fileWriter.write("invalid_option=true"); @@ -300,7 +298,7 @@ public void unknownOptionMustThrow() throws IOException { } @Test - public void tomlTableHeadingsMustBeIgnored() throws IOException { + public void tomlTableHeadingsMustBeIgnored(final @TempDir Path temp) throws IOException { when(mockCommandLine.getCommandSpec()).thenReturn(mockCommandSpec); @@ -310,7 +308,7 @@ public void tomlTableHeadingsMustBeIgnored() throws IOException { validOptionsMap.put("--onemore-valid-option", null); when(mockCommandSpec.optionsMap()).thenReturn(validOptionsMap); - final File tempConfigFile = temp.newFile("config.toml"); + final File tempConfigFile = temp.resolve("config.toml").toFile(); final BufferedWriter fileWriter = Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8); fileWriter.write("a-valid-option=123"); @@ -343,7 +341,8 @@ public void tomlTableHeadingsMustBeIgnored() throws IOException { } @Test - public void tomlTableHeadingsMustNotSkipValidationOfUnknownOptions() throws IOException { + public void tomlTableHeadingsMustNotSkipValidationOfUnknownOptions(final @TempDir Path temp) + throws IOException { when(mockCommandLine.getCommandSpec()).thenReturn(mockCommandSpec); @@ -351,7 +350,7 @@ public void tomlTableHeadingsMustNotSkipValidationOfUnknownOptions() throws IOEx validOptionsMap.put("--a-valid-option", null); when(mockCommandSpec.optionsMap()).thenReturn(validOptionsMap); - final File tempConfigFile = temp.newFile("config.toml"); + final File tempConfigFile = temp.resolve("config.toml").toFile(); final BufferedWriter fileWriter = Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8); fileWriter.write("[ignoreme]"); diff --git a/besu/src/test/java/org/hyperledger/besu/cli/util/ConfigOptionSearchAndRunHandlerTest.java b/besu/src/test/java/org/hyperledger/besu/cli/util/ConfigOptionSearchAndRunHandlerTest.java index 0f0c1e696a3..bd33703d9cc 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/util/ConfigOptionSearchAndRunHandlerTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/util/ConfigOptionSearchAndRunHandlerTest.java @@ -20,6 +20,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -28,18 +29,19 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; -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 picocli.CommandLine; import picocli.CommandLine.IDefaultValueProvider; import picocli.CommandLine.IExecutionStrategy; @@ -49,11 +51,11 @@ import picocli.CommandLine.ParseResult; import picocli.CommandLine.RunLast; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class ConfigOptionSearchAndRunHandlerTest { private static final String CONFIG_FILE_OPTION_NAME = "--config-file"; - @Rule public final TemporaryFolder temp = new TemporaryFolder(); + @TempDir public Path temp; private LoggingLevelOption levelOption; private final IExecutionStrategy resultHandler = new RunLast(); @@ -68,16 +70,18 @@ public class ConfigOptionSearchAndRunHandlerTest { @Mock IGetter mockConfigOptionGetter; @Mock BesuParameterExceptionHandler mockParameterExceptionHandler; - @Before + @BeforeEach public void initMocks() { - when(mockCommandSpec.commandLine()).thenReturn(mockCommandLine); - when(mockParseResult.commandSpec()).thenReturn(mockCommandSpec); + lenient().when(mockCommandSpec.commandLine()).thenReturn(mockCommandLine); + lenient().when(mockParseResult.commandSpec()).thenReturn(mockCommandSpec); final List originalArgs = new ArrayList<>(); originalArgs.add(CONFIG_FILE_OPTION_NAME); - when(mockParseResult.originalArgs()).thenReturn(originalArgs); - when(mockParseResult.matchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(mockConfigOptionSpec); - when(mockParseResult.hasMatchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(true); - when(mockConfigOptionSpec.getter()).thenReturn(mockConfigOptionGetter); + lenient().when(mockParseResult.originalArgs()).thenReturn(originalArgs); + lenient() + .when(mockParseResult.matchedOption(CONFIG_FILE_OPTION_NAME)) + .thenReturn(mockConfigOptionSpec); + lenient().when(mockParseResult.hasMatchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(true); + lenient().when(mockConfigOptionSpec.getter()).thenReturn(mockConfigOptionGetter); levelOption = new LoggingLevelOption(); levelOption.setLogLevel("INFO"); configParsingHandler = @@ -87,7 +91,7 @@ public void initMocks() { @Test public void handleWithCommandLineOption() throws Exception { - when(mockConfigOptionGetter.get()).thenReturn(temp.newFile()); + when(mockConfigOptionGetter.get()).thenReturn(Files.createTempFile("tmp", "txt").toFile()); final List result = configParsingHandler.handle(mockParseResult); verify(mockCommandLine).setDefaultValueProvider(any(IDefaultValueProvider.class)); verify(mockCommandLine).setExecutionStrategy(eq(resultHandler)); @@ -105,7 +109,9 @@ public void handleWithEnvironmentVariable() throws IOException { new ConfigOptionSearchAndRunHandler( resultHandler, mockParameterExceptionHandler, - singletonMap("BESU_CONFIG_FILE", temp.newFile().getAbsolutePath())); + singletonMap( + "BESU_CONFIG_FILE", + Files.createFile(temp.resolve("tmp")).toFile().getAbsolutePath())); when(mockParseResult.hasMatchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(false); @@ -160,7 +166,7 @@ public void handleThrowsErrorWithWithEnvironmentVariableAndCommandLineSpecified( new ConfigOptionSearchAndRunHandler( resultHandler, mockParameterExceptionHandler, - singletonMap("BESU_CONFIG_FILE", temp.newFile().getAbsolutePath())); + singletonMap("BESU_CONFIG_FILE", temp.resolve("tmp").toFile().getAbsolutePath())); when(mockParseResult.hasMatchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(true); diff --git a/besu/src/test/java/org/hyperledger/besu/services/BesuEventsImplTest.java b/besu/src/test/java/org/hyperledger/besu/services/BesuEventsImplTest.java index 6245a8a6ba8..25db1cb9b49 100644 --- a/besu/src/test/java/org/hyperledger/besu/services/BesuEventsImplTest.java +++ b/besu/src/test/java/org/hyperledger/besu/services/BesuEventsImplTest.java @@ -17,6 +17,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -76,15 +77,15 @@ import com.google.common.base.Supplier; import com.google.common.base.Suppliers; -import org.junit.Before; -import org.junit.Test; -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.mockito.Answers; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; @SuppressWarnings("unchecked") -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class BesuEventsImplTest { private static final Supplier SIGNATURE_ALGORITHM = @@ -113,7 +114,7 @@ public class BesuEventsImplTest { private MutableBlockchain blockchain; private final BlockDataGenerator gen = new BlockDataGenerator(); - @Before + @BeforeEach public void setUp() { blockchain = DefaultBlockchain.createMutable( @@ -128,18 +129,22 @@ public void setUp() { when(mockEthContext.getEthMessages()).thenReturn(mockEthMessages); when(mockEthContext.getEthPeers()).thenReturn(mockEthPeers); when(mockEthContext.getScheduler()).thenReturn(mockEthScheduler); - when(mockEthPeers.streamAvailablePeers()).thenAnswer(z -> Stream.empty()); + lenient().when(mockEthPeers.streamAvailablePeers()).thenAnswer(z -> Stream.empty()); when(mockProtocolContext.getBlockchain()).thenReturn(blockchain); - when(mockProtocolContext.getWorldStateArchive()).thenReturn(mockWorldStateArchive); - when(mockProtocolSchedule.getByBlockHeader(any())).thenReturn(mockProtocolSpec); - when(mockProtocolSpec.getTransactionValidatorFactory()) + lenient().when(mockProtocolContext.getWorldStateArchive()).thenReturn(mockWorldStateArchive); + lenient().when(mockProtocolSchedule.getByBlockHeader(any())).thenReturn(mockProtocolSpec); + lenient() + .when(mockProtocolSpec.getTransactionValidatorFactory()) .thenReturn(mockTransactionValidatorFactory); - when(mockProtocolSpec.getFeeMarket()).thenReturn(FeeMarket.london(0L)); - when(mockTransactionValidatorFactory.get().validate(any(), any(Optional.class), any())) + lenient().when(mockProtocolSpec.getFeeMarket()).thenReturn(FeeMarket.london(0L)); + lenient() + .when(mockTransactionValidatorFactory.get().validate(any(), any(Optional.class), any())) .thenReturn(ValidationResult.valid()); - when(mockTransactionValidatorFactory.get().validateForSender(any(), any(), any())) + lenient() + .when(mockTransactionValidatorFactory.get().validateForSender(any(), any(), any())) .thenReturn(ValidationResult.valid()); - when(mockWorldStateArchive.getMutable(any(), anyBoolean())) + lenient() + .when(mockWorldStateArchive.getMutable(any(), anyBoolean())) .thenReturn(Optional.of(mockWorldState)); blockBroadcaster = new BlockBroadcaster(mockEthContext); diff --git a/besu/src/test/java/org/hyperledger/besu/services/PicoCLIOptionsImplTest.java b/besu/src/test/java/org/hyperledger/besu/services/PicoCLIOptionsImplTest.java index ac7e7fb9593..52f37f769a9 100644 --- a/besu/src/test/java/org/hyperledger/besu/services/PicoCLIOptionsImplTest.java +++ b/besu/src/test/java/org/hyperledger/besu/services/PicoCLIOptionsImplTest.java @@ -17,16 +17,16 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; import picocli.CommandLine; import picocli.CommandLine.Command; import picocli.CommandLine.Option; import picocli.CommandLine.UnmatchedArgumentException; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class PicoCLIOptionsImplTest { @Command @@ -46,7 +46,7 @@ static final class MixinOptions { private CommandLine commandLine; private PicoCLIOptionsImpl serviceImpl; - @Before + @BeforeEach public void setUp() { command = new SimpleCommand(); mixin = new MixinOptions();