|
23 | 23 | import java.util.Map;
|
24 | 24 |
|
25 | 25 | import org.junit.jupiter.api.Test;
|
26 |
| -import org.mockito.Mockito; |
| 26 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 27 | +import org.mockito.MockedStatic; |
| 28 | +import org.mockito.junit.jupiter.MockitoExtension; |
27 | 29 |
|
28 | 30 | import org.springframework.cli.runtime.engine.model.ModelPopulator;
|
29 | 31 | import org.springframework.cli.runtime.engine.model.SystemModelPopulator;
|
30 | 32 | import org.springframework.cli.util.TerminalMessage;
|
31 | 33 | import org.springframework.shell.command.CommandRegistration;
|
32 | 34 |
|
33 | 35 | import static org.assertj.core.api.Assertions.assertThat;
|
| 36 | +import static org.mockito.Mockito.mockStatic; |
34 | 37 |
|
| 38 | +@ExtendWith(MockitoExtension.class) |
35 | 39 | class DynamicMethodCommandResolverTests {
|
36 | 40 |
|
37 | 41 | @Test
|
@@ -71,30 +75,32 @@ void testResolving() {
|
71 | 75 | modelPopulators.add(systemModelPopulator);
|
72 | 76 | DynamicMethodCommandResolver resolver = new DynamicMethodCommandResolver(modelPopulators,
|
73 | 77 | () -> CommandRegistration.builder(), TerminalMessage.noop(), null);
|
74 |
| - // Move to mock scan so that we control results |
75 |
| - DynamicMethodCommandResolver spy = Mockito.spy(resolver); |
76 |
| - Mockito.when(spy.scanCommands()).thenReturn(commandScanResults); |
77 |
| - List<CommandRegistration> resolved = spy.resolve(); |
78 |
| - |
79 |
| - assertThat(resolved).hasSize(2); |
80 |
| - assertThat(resolved).satisfiesExactly(registration -> { |
81 |
| - assertThat(registration.getCommand()).isEqualTo("k8s-simple new"); |
82 |
| - assertThat(registration.getDescription()).isEqualTo("subcommand description"); |
83 |
| - assertThat(registration.getOptions()).hasSize(2); |
84 |
| - assertThat(registration.getOptions().get(0)).satisfies(option -> { |
85 |
| - assertThat(option.getLongNames()).contains("with-gusto"); |
86 |
| - assertThat(option.getType().getType()).isEqualTo(Boolean.class); |
87 |
| - assertThat(option.getDescription()).isEqualTo("what a nice simple option"); |
88 |
| - assertThat(option.getDefaultValue()).isEqualTo("true"); |
89 |
| - assertThat(option.isRequired()).isTrue(); |
90 |
| - }); |
91 |
| - assertThat(registration.getOptions().get(1)).satisfies(option -> { |
92 |
| - assertThat(option.getLongNames()).contains("with-greeting"); |
93 |
| - assertThat(option.getType().getType()).isEqualTo(String.class); |
| 78 | + |
| 79 | + try (MockedStatic<CommandScanner> mockedCommandScanner = mockStatic(CommandScanner.class)) { |
| 80 | + mockedCommandScanner.when(CommandScanner::scanCommands).thenReturn(commandScanResults); |
| 81 | + |
| 82 | + List<CommandRegistration> resolved = resolver.resolve(); |
| 83 | + |
| 84 | + assertThat(resolved).hasSize(2); |
| 85 | + assertThat(resolved).satisfiesExactly(registration -> { |
| 86 | + assertThat(registration.getCommand()).isEqualTo("k8s-simple new"); |
| 87 | + assertThat(registration.getDescription()).isEqualTo("subcommand description"); |
| 88 | + assertThat(registration.getOptions()).hasSize(2); |
| 89 | + assertThat(registration.getOptions().get(0)).satisfies(option -> { |
| 90 | + assertThat(option.getLongNames()).contains("with-gusto"); |
| 91 | + assertThat(option.getType().getType()).isEqualTo(Boolean.class); |
| 92 | + assertThat(option.getDescription()).isEqualTo("what a nice simple option"); |
| 93 | + assertThat(option.getDefaultValue()).isEqualTo("true"); |
| 94 | + assertThat(option.isRequired()).isTrue(); |
| 95 | + }); |
| 96 | + assertThat(registration.getOptions().get(1)).satisfies(option -> { |
| 97 | + assertThat(option.getLongNames()).contains("with-greeting"); |
| 98 | + assertThat(option.getType().getType()).isEqualTo(String.class); |
| 99 | + }); |
| 100 | + }, registration -> { |
| 101 | + assertThat(registration.getCommand()).isEqualTo("k8s-simple new-services"); |
94 | 102 | });
|
95 |
| - }, registration -> { |
96 |
| - assertThat(registration.getCommand()).isEqualTo("k8s-simple new-services"); |
97 |
| - }); |
| 103 | + } |
98 | 104 | }
|
99 | 105 |
|
100 | 106 | }
|
0 commit comments