diff --git a/pom.xml b/pom.xml index a9c43ea2..12596dcd 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.6.RELEASE + 2.6.1 diff --git a/spring-shell-core/src/test/java/org/springframework/shell/ConfigurableCommandRegistryTest.java b/spring-shell-core/src/test/java/org/springframework/shell/ConfigurableCommandRegistryTest.java index f38dca02..62aee668 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/ConfigurableCommandRegistryTest.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/ConfigurableCommandRegistryTest.java @@ -16,13 +16,10 @@ package org.springframework.shell; -import static org.hamcrest.collection.IsMapContaining.hasEntry; -import static org.hamcrest.collection.IsMapContaining.hasKey; -import static org.junit.Assert.*; +import org.junit.jupiter.api.Test; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Unit tests for {@link ConfigurableCommandRegistry}. @@ -31,15 +28,11 @@ import org.junit.rules.ExpectedException; */ public class ConfigurableCommandRegistryTest { - @Rule - public ExpectedException thrown= ExpectedException.none(); - @Test public void testRegistration() { ConfigurableCommandRegistry registry = new ConfigurableCommandRegistry(); registry.register("foo", MethodTarget.of("toString", this, new Command.Help("some command"))); - - assertThat(registry.listCommands(), hasKey("foo")); + assertThat(registry.listCommands()).containsKeys("foo"); } @Test @@ -47,12 +40,11 @@ public class ConfigurableCommandRegistryTest { ConfigurableCommandRegistry registry = new ConfigurableCommandRegistry(); registry.register("foo", MethodTarget.of("toString", this, new Command.Help("some command"))); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("foo"); - thrown.expectMessage("toString"); - thrown.expectMessage("hashCode"); - - registry.register("foo", MethodTarget.of("hashCode", this, new Command.Help("some command"))); + assertThatThrownBy(() -> { + registry.register("foo", MethodTarget.of("hashCode", this, new Command.Help("some command"))); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("foo") + .hasMessageContaining("toString") + .hasMessageContaining("hashCode"); } - } diff --git a/spring-shell-core/src/test/java/org/springframework/shell/ShellTest.java b/spring-shell-core/src/test/java/org/springframework/shell/ShellTest.java index 357e75f4..a2a1f638 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/ShellTest.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/ShellTest.java @@ -22,21 +22,20 @@ import java.util.Collections; import java.util.List; import java.util.stream.Collectors; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnit; -import org.mockito.junit.MockitoRule; +import org.mockito.junit.jupiter.MockitoExtension; + import org.springframework.context.ApplicationContext; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.fail; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.isA; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -46,13 +45,12 @@ import static org.mockito.Mockito.when; * * @author Eric Bottard */ +@ExtendWith(MockitoExtension.class) +// @RunWith(JUnitPlatform.class) public class ShellTest { - @Rule - public MockitoRule mockitoRule = MockitoJUnit.rule(); - - @Rule - public ExpectedException thrown= ExpectedException.none(); + // @Rule + // public MockitoRule mockitoRule = MockitoJUnit.rule(); @Mock private InputProvider inputProvider; @@ -62,7 +60,7 @@ public class ShellTest { @Mock private ParameterResolver parameterResolver; - + private ValueResult valueResult; @InjectMocks @@ -70,7 +68,7 @@ public class ShellTest { private boolean invoked; - @Before + @BeforeEach public void setUp() { shell.parameterResolvers = Arrays.asList(parameterResolver); } @@ -93,7 +91,7 @@ public class ShellTest { } - Assert.assertTrue(invoked); + assertThat(invoked).isTrue(); } @Test @@ -147,12 +145,11 @@ public class ShellTest { } - Assert.assertTrue(invoked); + assertThat(invoked).isTrue(); } @Test public void commandThrowingAnException() throws IOException { - when(parameterResolver.supports(any())).thenReturn(true); when(inputProvider.readInput()).thenReturn(() -> "fail", null); doThrow(new Exit()).when(resultHandler).handleResult(isA(SomeException.class)); @@ -166,17 +163,14 @@ public class ShellTest { } - Assert.assertTrue(invoked); - + assertThat(invoked).isTrue(); } @Test public void comments() throws IOException { - when(parameterResolver.supports(any())).thenReturn(true); when(inputProvider.readInput()).thenReturn(() -> "// This is a comment", (Input) null); shell.run(inputProvider); - } @Test @@ -188,8 +182,9 @@ public class ShellTest { r.register("hw", MethodTarget.of("helloWorld", this, new Command.Help("hellow world"))); })); - thrown.expect(ParameterResolverMissingException.class); - shell.gatherMethodTargets(); + assertThatThrownBy(() -> { + shell.gatherMethodTargets(); + }).isInstanceOf(ParameterResolverMissingException.class); } @Test diff --git a/spring-shell-core/src/test/java/org/springframework/shell/UtilsTest.java b/spring-shell-core/src/test/java/org/springframework/shell/UtilsTest.java index 1d102c7e..9d8c542d 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/UtilsTest.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/UtilsTest.java @@ -16,9 +16,9 @@ package org.springframework.shell; -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; -import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link Utils}. diff --git a/spring-shell-jcommander-adapter/src/test/java/org/springframework/shell/jcommander/JCommanderParameterResolverTest.java b/spring-shell-jcommander-adapter/src/test/java/org/springframework/shell/jcommander/JCommanderParameterResolverTest.java index 5f915603..2189e041 100644 --- a/spring-shell-jcommander-adapter/src/test/java/org/springframework/shell/jcommander/JCommanderParameterResolverTest.java +++ b/spring-shell-jcommander-adapter/src/test/java/org/springframework/shell/jcommander/JCommanderParameterResolverTest.java @@ -16,16 +16,12 @@ package org.springframework.shell.jcommander; -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; -import static org.assertj.core.api.Assertions.assertThat; - import java.lang.reflect.Method; import java.util.Arrays; import java.util.Collections; import java.util.stream.Stream; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.core.MethodParameter; import org.springframework.shell.CompletionContext; @@ -34,6 +30,10 @@ import org.springframework.shell.ParameterDescription; import org.springframework.shell.Utils; import org.springframework.util.ReflectionUtils; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; + /** * Unit test for {@link JCommanderParameterResolver}. * diff --git a/spring-shell-samples/src/main/resources/application.yml b/spring-shell-samples/src/main/resources/application.yml new file mode 100644 index 00000000..2d5afe0b --- /dev/null +++ b/spring-shell-samples/src/main/resources/application.yml @@ -0,0 +1,3 @@ +spring: + main: + allow-circular-references: true \ No newline at end of file diff --git a/spring-shell-standard-commands/src/test/java/org/springframework/shell/standard/commands/HelpTest.java b/spring-shell-standard-commands/src/test/java/org/springframework/shell/standard/commands/HelpTest.java index b1259594..475037bb 100644 --- a/spring-shell-standard-commands/src/test/java/org/springframework/shell/standard/commands/HelpTest.java +++ b/spring-shell-standard-commands/src/test/java/org/springframework/shell/standard/commands/HelpTest.java @@ -19,18 +19,22 @@ package org.springframework.shell.standard.commands; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.lang.reflect.Method; import java.util.Collections; import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.Optional; + +import javax.validation.constraints.Max; import org.assertj.core.api.Assertions; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestName; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; @@ -38,47 +42,53 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.io.ClassPathResource; import org.springframework.shell.Command; -import org.springframework.shell.standard.StandardParameterResolver; +import org.springframework.shell.CommandRegistry; import org.springframework.shell.MethodTarget; import org.springframework.shell.ParameterResolver; -import org.springframework.shell.CommandRegistry; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; +import org.springframework.shell.standard.StandardParameterResolver; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.util.FileCopyUtils; -import javax.validation.constraints.Max; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Tests for the {@link Help} command. * * @author Eric Bottard */ -@RunWith(SpringJUnit4ClassRunner.class) +@ExtendWith(SpringExtension.class) @ContextConfiguration(classes = HelpTest.Config.class) public class HelpTest { private static Locale previousLocale; + private String testName; - @BeforeClass + @BeforeAll public static void setAssumedLocale() { previousLocale = Locale.getDefault(); Locale.setDefault(Locale.ENGLISH); } - @AfterClass + @AfterAll public static void restorePreviousLocale() { Locale.setDefault(previousLocale); } + @BeforeEach + public void setup(TestInfo testInfo) { + Optional testMethod = testInfo.getTestMethod(); + if (testMethod.isPresent()) { + this.testName = testMethod.get().getName(); + } + } + @Autowired private Help help; - @Rule - public TestName testName = new TestName(); - @Test public void testCommandHelp() throws Exception { CharSequence help = this.help.help("first-command").toString(); @@ -91,13 +101,15 @@ public class HelpTest { Assertions.assertThat(list).isEqualTo(sample()); } - @Test(expected = IllegalArgumentException.class) + @Test public void testUnknownCommand() throws Exception { - this.help.help("some unknown command"); + assertThatThrownBy(() -> { + this.help.help("some unknown command"); + }).isInstanceOf(IllegalArgumentException.class); } private String sample() throws IOException { - InputStream is = new ClassPathResource(HelpTest.class.getSimpleName() + "-" + testName.getMethodName() + ".txt", HelpTest.class).getInputStream(); + InputStream is = new ClassPathResource(HelpTest.class.getSimpleName() + "-" + testName + ".txt", HelpTest.class).getInputStream(); return FileCopyUtils.copyToString(new InputStreamReader(is, "UTF-8")).replace("&", ""); } diff --git a/spring-shell-standard/src/test/java/org/springframework/shell/standard/CommandValueProviderTest.java b/spring-shell-standard/src/test/java/org/springframework/shell/standard/CommandValueProviderTest.java index 631b9376..698c81a9 100644 --- a/spring-shell-standard/src/test/java/org/springframework/shell/standard/CommandValueProviderTest.java +++ b/spring-shell-standard/src/test/java/org/springframework/shell/standard/CommandValueProviderTest.java @@ -17,28 +17,28 @@ package org.springframework.shell.standard; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.when; - import java.lang.reflect.Method; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.core.MethodParameter; +import org.springframework.shell.CommandRegistry; import org.springframework.shell.CompletionContext; import org.springframework.shell.CompletionProposal; import org.springframework.shell.MethodTarget; -import org.springframework.shell.CommandRegistry; import org.springframework.shell.Utils; import org.springframework.util.ReflectionUtils; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + /** * Unit tests for {@link CommandValueProvider}. * @@ -49,7 +49,7 @@ public class CommandValueProviderTest { @Mock private CommandRegistry shell; - @Before + @BeforeEach public void setUp() { MockitoAnnotations.initMocks(this); } diff --git a/spring-shell-standard/src/test/java/org/springframework/shell/standard/StandardMethodTargetRegistrarTest.java b/spring-shell-standard/src/test/java/org/springframework/shell/standard/StandardMethodTargetRegistrarTest.java index f48f0000..eb561f48 100644 --- a/spring-shell-standard/src/test/java/org/springframework/shell/standard/StandardMethodTargetRegistrarTest.java +++ b/spring-shell-standard/src/test/java/org/springframework/shell/standard/StandardMethodTargetRegistrarTest.java @@ -16,26 +16,23 @@ package org.springframework.shell.standard; +import java.util.Map; + import org.assertj.core.api.Assertions; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; + import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.shell.Availability; import org.springframework.shell.ConfigurableCommandRegistry; import org.springframework.shell.MethodTarget; import org.springframework.shell.standard.test1.GroupOneCommands; -import org.springframework.shell.standard.test2.GroupTwoCommands; import org.springframework.shell.standard.test2.GroupThreeCommands; +import org.springframework.shell.standard.test2.GroupTwoCommands; import org.springframework.util.ReflectionUtils; -import static org.hamcrest.Matchers.hasEntry; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertThat; - -import java.util.Map; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Unit tests for {@link StandardMethodTargetRegistrar}. @@ -47,9 +44,6 @@ public class StandardMethodTargetRegistrarTest { private StandardMethodTargetRegistrar registrar = new StandardMethodTargetRegistrar(); private ConfigurableCommandRegistry registry = new ConfigurableCommandRegistry(); - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void testRegistrations() { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Sample.class); @@ -57,21 +51,21 @@ public class StandardMethodTargetRegistrarTest { registrar.register(registry); MethodTarget methodTarget = registry.listCommands().get("say-hello"); - assertThat(methodTarget, notNullValue()); - assertThat(methodTarget.getHelp(), is("some command")); - assertThat(methodTarget.getMethod(), is(ReflectionUtils.findMethod(Sample.class, "sayHello", String.class))); - assertThat(methodTarget.getAvailability().isAvailable(), is(true)); + assertThat(methodTarget).isNotNull(); + assertThat(methodTarget.getHelp()).isEqualTo("some command"); + assertThat(methodTarget.getMethod()).isEqualTo(ReflectionUtils.findMethod(Sample.class, "sayHello", String.class)); + assertThat(methodTarget.getAvailability().isAvailable()).isTrue(); methodTarget = registry.listCommands().get("hi"); - assertThat(methodTarget, notNullValue()); - assertThat(methodTarget.getHelp(), is("method with alias")); - assertThat(methodTarget.getMethod(), is(ReflectionUtils.findMethod(Sample.class, "greet", String.class))); - assertThat(methodTarget.getAvailability().isAvailable(), is(true)); + assertThat(methodTarget).isNotNull(); + assertThat(methodTarget.getHelp()).isEqualTo("method with alias"); + assertThat(methodTarget.getMethod()).isEqualTo(ReflectionUtils.findMethod(Sample.class, "greet", String.class)); + assertThat(methodTarget.getAvailability().isAvailable()).isTrue(); methodTarget = registry.listCommands().get("alias"); - assertThat(methodTarget, notNullValue()); - assertThat(methodTarget.getHelp(), is("method with alias")); - assertThat(methodTarget.getMethod(), is(ReflectionUtils.findMethod(Sample.class, "greet", String.class))); - assertThat(methodTarget.getAvailability().isAvailable(), is(true)); + assertThat(methodTarget).isNotNull(); + assertThat(methodTarget.getHelp()).isEqualTo("method with alias"); + assertThat(methodTarget.getMethod()).isEqualTo(ReflectionUtils.findMethod(Sample.class, "greet", String.class)); + assertThat(methodTarget.getAvailability().isAvailable()).isTrue(); } @ShellComponent @@ -96,27 +90,27 @@ public class StandardMethodTargetRegistrarTest { SampleWithAvailability sample = applicationContext.getBean(SampleWithAvailability.class); MethodTarget methodTarget = registry.listCommands().get("say-hello"); - assertThat(methodTarget.getMethod(), is(ReflectionUtils.findMethod(SampleWithAvailability.class, "sayHello"))); - assertThat(methodTarget.getAvailability().isAvailable(), is(true)); + assertThat(methodTarget.getMethod()).isEqualTo(ReflectionUtils.findMethod(SampleWithAvailability.class, "sayHello")); + assertThat(methodTarget.getAvailability().isAvailable()).isTrue(); sample.available = false; - assertThat(methodTarget.getAvailability().isAvailable(), is(false)); - assertThat(methodTarget.getAvailability().getReason(), is("sayHelloAvailability")); + assertThat(methodTarget.getAvailability().isAvailable()).isFalse(); + assertThat(methodTarget.getAvailability().getReason()).isEqualTo("sayHelloAvailability"); sample.available = true; methodTarget = registry.listCommands().get("hi"); - assertThat(methodTarget.getMethod(), is(ReflectionUtils.findMethod(SampleWithAvailability.class, "hi"))); - assertThat(methodTarget.getAvailability().isAvailable(), is(true)); + assertThat(methodTarget.getMethod()).isEqualTo(ReflectionUtils.findMethod(SampleWithAvailability.class, "hi")); + assertThat(methodTarget.getAvailability().isAvailable()).isTrue(); sample.available = false; - assertThat(methodTarget.getAvailability().isAvailable(), is(false)); - assertThat(methodTarget.getAvailability().getReason(), is("customAvailabilityMethod")); + assertThat(methodTarget.getAvailability().isAvailable()).isFalse(); + assertThat(methodTarget.getAvailability().getReason()).isEqualTo("customAvailabilityMethod"); sample.available = true; methodTarget = registry.listCommands().get("bonjour"); - assertThat(methodTarget.getMethod(), is(ReflectionUtils.findMethod(SampleWithAvailability.class, "bonjour"))); - assertThat(methodTarget.getAvailability().isAvailable(), is(true)); + assertThat(methodTarget.getMethod()).isEqualTo(ReflectionUtils.findMethod(SampleWithAvailability.class, "bonjour")); + assertThat(methodTarget.getAvailability().isAvailable()).isTrue(); sample.available = false; - assertThat(methodTarget.getAvailability().isAvailable(), is(false)); - assertThat(methodTarget.getAvailability().getReason(), is("availabilityForSeveralCommands")); + assertThat(methodTarget.getAvailability().isAvailable()).isFalse(); + assertThat(methodTarget.getAvailability().getReason()).isEqualTo("availabilityForSeveralCommands"); sample.available = true; } @@ -169,12 +163,12 @@ public class StandardMethodTargetRegistrarTest { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(WrongAvailabilityIndicatorOnShellMethod.class); registrar.setApplicationContext(applicationContext); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("When set on a @ShellMethod method, the value of the @ShellMethodAvailability should be a single element"); - thrown.expectMessage("Found [one, two]"); - thrown.expectMessage("wrong()"); - - registrar.register(registry); + assertThatThrownBy(() -> { + registrar.register(registry); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("When set on a @ShellMethod method, the value of the @ShellMethodAvailability should be a single element") + .hasMessageContaining("Found [one, two]") + .hasMessageContaining("wrong()"); } @ShellComponent @@ -192,11 +186,11 @@ public class StandardMethodTargetRegistrarTest { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(WrongAvailabilityIndicatorWildcardNotAlone.class); registrar.setApplicationContext(applicationContext); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("When using '*' as a wildcard for ShellMethodAvailability, this can be the only value. Found [one, *]"); - thrown.expectMessage("availability()"); - - registrar.register(registry); + assertThatThrownBy(() -> { + registrar.register(registry); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("When using '*' as a wildcard for ShellMethodAvailability, this can be the only value. Found [one, *]") + .hasMessageContaining("availability()"); } @ShellComponent @@ -218,13 +212,13 @@ public class StandardMethodTargetRegistrarTest { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(WrongAvailabilityIndicatorAmbiguous.class); registrar.setApplicationContext(applicationContext); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Found several @ShellMethodAvailability"); - thrown.expectMessage("wrong()"); - thrown.expectMessage("availability()"); - thrown.expectMessage("otherAvailability()"); - - registrar.register(registry); + assertThatThrownBy(() -> { + registrar.register(registry); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Found several @ShellMethodAvailability") + .hasMessageContaining("wrong()") + .hasMessageContaining("availability()") + .hasMessageContaining("otherAvailability()"); } @ShellComponent diff --git a/spring-shell-standard/src/test/java/org/springframework/shell/standard/StandardParameterResolverTest.java b/spring-shell-standard/src/test/java/org/springframework/shell/standard/StandardParameterResolverTest.java index e09742e8..512474a9 100644 --- a/spring-shell-standard/src/test/java/org/springframework/shell/standard/StandardParameterResolverTest.java +++ b/spring-shell-standard/src/test/java/org/springframework/shell/standard/StandardParameterResolverTest.java @@ -16,21 +16,14 @@ package org.springframework.shell.standard; -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; -import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.shell.ValueResultAsserts.assertThat; -import static org.springframework.util.ReflectionUtils.findMethod; - import java.lang.reflect.Method; import java.util.List; import java.util.stream.Collectors; import org.jline.reader.ParsedLine; import org.jline.reader.impl.DefaultParser; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; + import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.shell.CompletionContext; import org.springframework.shell.CompletionProposal; @@ -39,6 +32,13 @@ import org.springframework.shell.UnfinishedParameterResolutionException; import org.springframework.shell.Utils; import org.springframework.shell.ValueResult; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.springframework.shell.ValueResultAsserts.assertThat; +import static org.springframework.util.ReflectionUtils.findMethod; + /** * Unit tests for DefaultParameterResolver. * @author Eric Bottard @@ -46,9 +46,6 @@ import org.springframework.shell.ValueResult; */ public class StandardParameterResolverTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - private StandardParameterResolver resolver = new StandardParameterResolver(new DefaultConversionService()); // Tests for resolution @@ -61,16 +58,16 @@ public class StandardParameterResolverTest { ValueResult result0 = resolver.resolve(Utils.createMethodParameter(method, 0), words); assertThat(result0).hasValue(true).usesWords(0).notUsesWordsForValue(); assertThat(result0.wordsUsed(words)).containsExactly("--force"); - + ValueResult result1 = resolver.resolve(Utils.createMethodParameter(method, 1), words); assertThat(result1).hasValue("--foo").usesWords(1, 2).usesWordsForValue(2); assertThat(result1.wordsUsed(words)).containsExactly("--name", "--foo"); assertThat(result1.wordsUsedForValue(words)).containsExactly("--foo"); - + ValueResult result2 = resolver.resolve(Utils.createMethodParameter(method, 2), words); assertThat(result2).hasValue("y").usesWords(3).usesWordsForValue(3); assertThat(result2.wordsUsed(words)).containsExactly("y"); - + ValueResult result3 = resolver.resolve(Utils.createMethodParameter(method, 3), words); assertThat(result3).hasValue("last").notUsesWords().notUsesWordsForValue(); } @@ -88,78 +85,72 @@ public class StandardParameterResolverTest { public void testParameterSpecifiedTwiceViaDifferentAliases() throws Exception { Method method = findMethod(Remote.class, "zap", boolean.class, String.class, String.class, String.class); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Named parameter has been specified multiple times via '--bar, --baz'"); - - resolver.resolve( - Utils.createMethodParameter(method, 0), - asList("--force --name --foo y --bar x --baz z".split(" ")) - ); + assertThatThrownBy(() -> { + resolver.resolve( + Utils.createMethodParameter(method, 0), + asList("--force --name --foo y --bar x --baz z".split(" "))); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Named parameter has been specified multiple times via '--bar, --baz'"); } @Test public void testParameterSpecifiedTwiceViaSameKey() throws Exception { Method method = findMethod(Remote.class, "zap", boolean.class, String.class, String.class, String.class); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Parameter for '--baz' has already been specified"); - - resolver.resolve( - Utils.createMethodParameter(method, 0), - asList("--force --name --foo y --baz x --baz z".split(" ")) - ); + assertThatThrownBy(() -> { + resolver.resolve( + Utils.createMethodParameter(method, 0), + asList("--force --name --foo y --baz x --baz z".split(" "))); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Parameter for '--baz' has already been specified"); } @Test public void testTooMuchInput() throws Exception { Method method = findMethod(Remote.class, "zap", boolean.class, String.class, String.class, String.class); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("the following could not be mapped to parameters: 'leftover'"); - - resolver.resolve( - Utils.createMethodParameter(method, 0), - asList("--foo hello --name bar --force --bar well leftover".split(" ")) - ); + assertThatThrownBy(() -> { + resolver.resolve( + Utils.createMethodParameter(method, 0), + asList("--foo hello --name bar --force --bar well leftover".split(" "))); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("the following could not be mapped to parameters: 'leftover'"); } @Test public void testIncompleteCommandResolution() throws Exception { Method method = findMethod(Remote.class, "shutdown", Remote.Delay.class); - thrown.expect(UnfinishedParameterResolutionException.class); - thrown.expectMessage("Error trying to resolve '--delay delay' using [--delay]"); - - resolver.resolve( - Utils.createMethodParameter(method, 0), - asList("--delay".split(" ")) - ); + assertThatThrownBy(() -> { + resolver.resolve( + Utils.createMethodParameter(method, 0), + asList("--delay".split(" "))); + }).isInstanceOf(UnfinishedParameterResolutionException.class) + .hasMessageContaining("Error trying to resolve '--delay delay' using [--delay]"); } @Test public void testIncompleteCommandResolutionBigArity() throws Exception { Method method = findMethod(Remote.class, "add", List.class); - thrown.expect(UnfinishedParameterResolutionException.class); - thrown.expectMessage("Error trying to resolve '--numbers list list list' using [--numbers 1 2]"); - - resolver.resolve( - Utils.createMethodParameter(method, 0), - asList("--numbers 1 2".split(" ")) - ); + assertThatThrownBy(() -> { + resolver.resolve( + Utils.createMethodParameter(method, 0), + asList("--numbers 1 2".split(" "))); + }).isInstanceOf(UnfinishedParameterResolutionException.class) + .hasMessageContaining("Error trying to resolve '--numbers list list list' using [--numbers 1 2]"); } @Test public void testUnresolvableArg() throws Exception { Method method = findMethod(Remote.class, "zap", boolean.class, String.class, String.class, String.class); - thrown.expect(ParameterMissingResolutionException.class); - thrown.expectMessage("Parameter '--name string' should be specified"); - - resolver.resolve( - Utils.createMethodParameter(method, 1), - asList("--foo hello --force --bar well".split(" ")) - ); + assertThatThrownBy(() -> { + resolver.resolve( + Utils.createMethodParameter(method, 1), + asList("--foo hello --force --bar well".split(" "))); + }).isInstanceOf(ParameterMissingResolutionException.class) + .hasMessageContaining("Parameter '--name string' should be specified"); } // Tests for completion @@ -216,9 +207,9 @@ public class StandardParameterResolverTest { @Test public void testValueCompletionWithNonDefaultArity() { - + resolver.setValueProviders(singletonList(new Remote.NumberValueProvider("12", "42", "7"))); - + Method[] methods = { findMethod(org.springframework.shell.standard.Remote.class, "add", List.class), findMethod(org.springframework.shell.standard.Remote.class, "addAsArray", int[].class), diff --git a/spring-shell-test-samples/src/test/java/com/example/test/functional/CalculatorCommandsTest.java b/spring-shell-test-samples/src/test/java/com/example/test/functional/CalculatorCommandsTest.java index 9ce94123..647065fa 100644 --- a/spring-shell-test-samples/src/test/java/com/example/test/functional/CalculatorCommandsTest.java +++ b/spring-shell-test-samples/src/test/java/com/example/test/functional/CalculatorCommandsTest.java @@ -1,23 +1,23 @@ package com.example.test.functional; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsNull.notNullValue; -import static org.junit.Assert.assertThat; -import static org.springframework.util.ReflectionUtils.findMethod; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import com.example.CalculatorCommands; +import com.example.CalculatorState; +import com.example.test.BaseCalculatorTest; +import com.example.test.TestCalculatorStateConfig; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.shell.ConfigurableCommandRegistry; import org.springframework.shell.MethodTarget; import org.springframework.shell.standard.StandardMethodTargetRegistrar; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.example.CalculatorCommands; -import com.example.CalculatorState; -import com.example.test.BaseCalculatorTest; -import com.example.test.TestCalculatorStateConfig; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.util.ReflectionUtils.findMethod; /** * Illustrative functional tests for the Spring Shell Calculator application. These @@ -26,7 +26,7 @@ import com.example.test.TestCalculatorStateConfig; * * @author Sualeh Fatehi */ -@RunWith(SpringJUnit4ClassRunner.class) +@ExtendWith(SpringExtension.class) @ContextConfiguration(classes = { TestCalculatorStateConfig.class, CalculatorCommands.class }) public class CalculatorCommandsTest extends BaseCalculatorTest { @@ -40,7 +40,7 @@ public class CalculatorCommandsTest extends BaseCalculatorTest { @Autowired private ApplicationContext context; - @Before + @BeforeEach public void setup() { final StandardMethodTargetRegistrar registrar = new StandardMethodTargetRegistrar(); registrar.setApplicationContext(context); @@ -55,14 +55,15 @@ public class CalculatorCommandsTest extends BaseCalculatorTest { final String commandMethod = "add"; final MethodTarget commandTarget = lookupCommand(registry, command); - assertThat(commandTarget, notNullValue()); - assertThat(commandTarget.getGroup(), is("Calculator Commands")); - assertThat(commandTarget.getHelp(), is("Add two integers")); - assertThat(commandTarget.getMethod(), - is(findMethod(COMMAND_CLASS_UNDER_TEST, commandMethod, int.class, int.class))); - assertThat(commandTarget.getAvailability().isAvailable(), is(true)); - assertThat(invoke(commandTarget, 1, 2), is(3)); - assertThat(state.getMemory(), is(0)); + assertThat(commandTarget).isNotNull(); + assertThat(commandTarget.getGroup()).isEqualTo("Calculator Commands"); + assertThat(commandTarget.getHelp()).isEqualTo("Add two integers"); + assertThat(commandTarget.getMethod()) + .isEqualTo(findMethod(COMMAND_CLASS_UNDER_TEST, commandMethod, int.class, int.class)); + assertThat(commandTarget.getAvailability().isAvailable()).isTrue(); + Object invoke = invoke(commandTarget, 1, 2); + assertThat(invoke).isEqualTo(3); + assertThat(state.getMemory()).isEqualTo(0); } @Test @@ -71,16 +72,16 @@ public class CalculatorCommandsTest extends BaseCalculatorTest { final String commandMethod = "addToMemory"; final MethodTarget commandTarget = lookupCommand(registry, command); - assertThat(commandTarget, notNullValue()); - assertThat(commandTarget.getGroup(), is("Calculator Commands")); - assertThat(commandTarget.getHelp(), is("Add an integer to the value in memory")); - assertThat(commandTarget.getMethod(), - is(findMethod(COMMAND_CLASS_UNDER_TEST, commandMethod, int.class))); - assertThat(commandTarget.getAvailability().isAvailable(), is(true)); + assertThat(commandTarget).isNotNull(); + assertThat(commandTarget.getGroup()).isEqualTo("Calculator Commands"); + assertThat(commandTarget.getHelp()).isEqualTo("Add an integer to the value in memory"); + assertThat(commandTarget.getMethod()).isEqualTo(findMethod(COMMAND_CLASS_UNDER_TEST, commandMethod, int.class)); + assertThat(commandTarget.getAvailability().isAvailable()).isTrue(); state.setMemory(1); - assertThat(invoke(commandTarget, 2), is(3)); - assertThat(state.getMemory(), is(3)); + Object invoke = invoke(commandTarget, 2); + assertThat(invoke).isEqualTo(3); + assertThat(state.getMemory()).isEqualTo(3); } } diff --git a/spring-shell-test-samples/src/test/java/com/example/test/integration/CalculatorCommandsIntegrationTest.java b/spring-shell-test-samples/src/test/java/com/example/test/integration/CalculatorCommandsIntegrationTest.java index 6b08c9d8..02a61dec 100644 --- a/spring-shell-test-samples/src/test/java/com/example/test/integration/CalculatorCommandsIntegrationTest.java +++ b/spring-shell-test-samples/src/test/java/com/example/test/integration/CalculatorCommandsIntegrationTest.java @@ -1,22 +1,20 @@ package com.example.test.integration; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsNull.notNullValue; -import static org.junit.Assert.assertThat; -import static org.springframework.util.ReflectionUtils.findMethod; -import org.junit.Test; -import org.junit.runner.RunWith; +import com.example.CalculatorCommands; +import com.example.CalculatorState; +import com.example.test.BaseCalculatorTest; +import com.example.test.TestCalculatorStateConfig; +import org.junit.jupiter.api.Test; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.shell.MethodTarget; import org.springframework.shell.Shell; import org.springframework.shell.jline.InteractiveShellApplicationRunner; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.example.CalculatorCommands; -import com.example.CalculatorState; -import com.example.test.BaseCalculatorTest; -import com.example.test.TestCalculatorStateConfig; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.util.ReflectionUtils.findMethod; /** * @@ -25,8 +23,8 @@ import com.example.test.TestCalculatorStateConfig; * * @author Sualeh Fatehi */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(properties = { InteractiveShellApplicationRunner.SPRING_SHELL_INTERACTIVE_ENABLED + "=" + false }) +@SpringBootTest(properties = { InteractiveShellApplicationRunner.SPRING_SHELL_INTERACTIVE_ENABLED + "=" + false, + "spring.main.allow-circular-references=true" }) @ContextConfiguration(classes = TestCalculatorStateConfig.class) public class CalculatorCommandsIntegrationTest extends BaseCalculatorTest { @@ -48,14 +46,15 @@ public class CalculatorCommandsIntegrationTest extends BaseCalculatorTest { final String commandMethod = "add"; final MethodTarget commandTarget = lookupCommand(shell, command); - assertThat(commandTarget, notNullValue()); - assertThat(commandTarget.getGroup(), is("Calculator Commands")); - assertThat(commandTarget.getHelp(), is("Add two integers")); - assertThat(commandTarget.getMethod(), - is(findMethod(COMMAND_CLASS_UNDER_TEST, commandMethod, int.class, int.class))); - assertThat(commandTarget.getAvailability().isAvailable(), is(true)); - assertThat(shell.evaluate(() -> command + " 1 2"), is(3)); - assertThat(state.getMemory(), is(0)); + assertThat(commandTarget).isNotNull(); + assertThat(commandTarget.getGroup()).isEqualTo("Calculator Commands"); + assertThat(commandTarget.getHelp()).isEqualTo("Add two integers"); + assertThat(commandTarget.getMethod()) + .isEqualTo(findMethod(COMMAND_CLASS_UNDER_TEST, commandMethod, int.class, int.class)); + assertThat(commandTarget.getAvailability().isAvailable()).isTrue(); + Object evaluate = shell.evaluate(() -> command + " 1 2"); + assertThat(evaluate).isEqualTo(3); + assertThat(state.getMemory()).isEqualTo(0); } /** @@ -69,16 +68,16 @@ public class CalculatorCommandsIntegrationTest extends BaseCalculatorTest { final String commandMethod = "addToMemory"; final MethodTarget commandTarget = lookupCommand(shell, command); - assertThat(commandTarget, notNullValue()); - assertThat(commandTarget.getGroup(), is("Calculator Commands")); - assertThat(commandTarget.getHelp(), is("Add an integer to the value in memory")); - assertThat(commandTarget.getMethod(), - is(findMethod(COMMAND_CLASS_UNDER_TEST, commandMethod, int.class))); - assertThat(commandTarget.getAvailability().isAvailable(), is(true)); + assertThat(commandTarget).isNotNull(); + assertThat(commandTarget.getGroup()).isEqualTo("Calculator Commands"); + assertThat(commandTarget.getHelp()).isEqualTo("Add an integer to the value in memory"); + assertThat(commandTarget.getMethod()).isEqualTo(findMethod(COMMAND_CLASS_UNDER_TEST, commandMethod, int.class)); + assertThat(commandTarget.getAvailability().isAvailable()).isTrue(); state.setMemory(1); - assertThat(shell.evaluate(() -> command + " 2"), is(3)); - assertThat(state.getMemory(), is(3)); + Object evaluate = shell.evaluate(() -> command + " 2"); + assertThat(evaluate).isEqualTo(3); + assertThat(state.getMemory()).isEqualTo(3); } } diff --git a/spring-shell-test-samples/src/test/java/com/example/test/unit/AddTest.java b/spring-shell-test-samples/src/test/java/com/example/test/unit/AddTest.java index f22e1a4d..623af955 100644 --- a/spring-shell-test-samples/src/test/java/com/example/test/unit/AddTest.java +++ b/spring-shell-test-samples/src/test/java/com/example/test/unit/AddTest.java @@ -1,10 +1,10 @@ package com.example.test.unit; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; -import org.junit.Before; -import org.junit.Test; import com.example.CalculatorCommands; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; /** * @@ -22,7 +22,7 @@ public class AddTest { * Setup test calculator commands as "plain-old Java objects" or POJOs, initialized for * each test. */ - @Before + @BeforeEach public void setup() { commands = new CalculatorCommands(); } @@ -32,9 +32,9 @@ public class AddTest { */ @Test public void addHappyPath() { - assertThat(commands.add(0, 1), is(1)); - assertThat(commands.add(1, 2), is(3)); - assertThat(commands.add(1, 0), is(1)); + assertThat(commands.add(0, 1)).isEqualTo(1); + assertThat(commands.add(1, 2)).isEqualTo(3); + assertThat(commands.add(1, 0)).isEqualTo(1); } /** @@ -42,9 +42,9 @@ public class AddTest { */ @Test public void addNegatives() { - assertThat(commands.add(0, -1), is(-1)); - assertThat(commands.add(1, -2), is(-1)); - assertThat(commands.add(-1, 0), is(-1)); + assertThat(commands.add(0, -1)).isEqualTo(-1); + assertThat(commands.add(1, -2)).isEqualTo(-1); + assertThat(commands.add(-1, 0)).isEqualTo(-1); } } diff --git a/spring-shell-test-samples/src/test/java/com/example/test/unit/AddToMemoryTest.java b/spring-shell-test-samples/src/test/java/com/example/test/unit/AddToMemoryTest.java index 0eb9a503..ce8dcf25 100644 --- a/spring-shell-test-samples/src/test/java/com/example/test/unit/AddToMemoryTest.java +++ b/spring-shell-test-samples/src/test/java/com/example/test/unit/AddToMemoryTest.java @@ -1,15 +1,16 @@ package com.example.test.unit; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import java.lang.reflect.Field; + +import com.example.CalculatorCommands; +import com.example.CalculatorState; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.util.ReflectionUtils.findField; import static org.springframework.util.ReflectionUtils.makeAccessible; import static org.springframework.util.ReflectionUtils.setField; -import java.lang.reflect.Field; -import org.junit.Before; -import org.junit.Test; -import com.example.CalculatorCommands; -import com.example.CalculatorState; /** * @@ -29,7 +30,7 @@ public class AddToMemoryTest { * Setup test calculator commands as "plain-old Java objects" or POJOs, initialized for * each test. */ - @Before + @BeforeEach public void setup() { state = new CalculatorState(); commands = new CalculatorCommands(); @@ -44,14 +45,14 @@ public class AddToMemoryTest { */ @Test public void addToMemoryHappyPath() { - assertThat(commands.addToMemory(1), is(1)); - assertThat(state.getMemory(), is(1)); + assertThat(commands.addToMemory(1)).isEqualTo(1); + assertThat(state.getMemory()).isEqualTo(1); - assertThat(commands.addToMemory(2), is(3)); - assertThat(state.getMemory(), is(3)); + assertThat(commands.addToMemory(2)).isEqualTo(3); + assertThat(state.getMemory()).isEqualTo(3); - assertThat(commands.addToMemory(0), is(3)); - assertThat(state.getMemory(), is(3)); + assertThat(commands.addToMemory(0)).isEqualTo(3); + assertThat(state.getMemory()).isEqualTo(3); } /** @@ -60,14 +61,14 @@ public class AddToMemoryTest { */ @Test public void addToMemoryNegatives() { - assertThat(commands.addToMemory(-1), is(-1)); - assertThat(state.getMemory(), is(-1)); + assertThat(commands.addToMemory(-1)).isEqualTo(-1); + assertThat(state.getMemory()).isEqualTo(-1); - assertThat(commands.addToMemory(-2), is(-3)); - assertThat(state.getMemory(), is(-3)); + assertThat(commands.addToMemory(-2)).isEqualTo(-3); + assertThat(state.getMemory()).isEqualTo(-3); - assertThat(commands.addToMemory(0), is(-3)); - assertThat(state.getMemory(), is(-3)); + assertThat(commands.addToMemory(0)).isEqualTo(-3); + assertThat(state.getMemory()).isEqualTo(-3); } }