diff options
author | Dejan Milisavljevic <130993898+dejan-milisavljevic-sonarsource@users.noreply.github.com> | 2024-02-21 11:20:59 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-02-21 20:02:34 +0000 |
commit | 076a7616dda6fb07d9c779a55506494c20bfe9d4 (patch) | |
tree | f06cae9cde3c216802d258f9a4bd3a62b87d0355 /sonar-scanner-engine/src/test/java/org/sonar/scanner/qualitygate | |
parent | b9fdc3abf600f2ef02c0f4b5321fb7aae0140ac6 (diff) | |
download | sonarqube-076a7616dda6fb07d9c779a55506494c20bfe9d4.tar.gz sonarqube-076a7616dda6fb07d9c779a55506494c20bfe9d4.zip |
SONAR-21643 Convert some parameterized tests to Junit 5 (#10666)
Diffstat (limited to 'sonar-scanner-engine/src/test/java/org/sonar/scanner/qualitygate')
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/scanner/qualitygate/QualityGateCheckTest.java | 109 |
1 files changed, 62 insertions, 47 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/qualitygate/QualityGateCheckTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/qualitygate/QualityGateCheckTest.java index 2c8b7985c9c..89013ebcbc9 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/qualitygate/QualityGateCheckTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/qualitygate/QualityGateCheckTest.java @@ -19,17 +19,19 @@ */ package org.sonar.scanner.qualitygate; -import com.tngtech.java.junit.dataprovider.DataProvider; -import com.tngtech.java.junit.dataprovider.DataProviderRunner; -import com.tngtech.java.junit.dataprovider.UseDataProvider; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; +import java.util.stream.Stream; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.Answers; import org.mockito.ArgumentMatcher; -import org.mockito.Mockito; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.event.Level; -import org.sonar.api.testfixtures.log.LogTester; +import org.sonar.api.testfixtures.log.LogTesterJUnit5; import org.sonar.api.utils.MessageException; import org.sonar.scanner.bootstrap.DefaultScannerWsClient; import org.sonar.scanner.bootstrap.GlobalAnalysisMode; @@ -48,30 +50,34 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -@RunWith(DataProviderRunner.class) -public class QualityGateCheckTest { - private DefaultScannerWsClient wsClient = mock(DefaultScannerWsClient.class, Mockito.RETURNS_DEEP_STUBS); - private GlobalAnalysisMode analysisMode = mock(GlobalAnalysisMode.class); - private CeTaskReportDataHolder reportMetadataHolder = mock(CeTaskReportDataHolder.class); - private ScanProperties properties = mock(ScanProperties.class); - - @Rule - public LogTester logTester = new LogTester(); - - QualityGateCheck underTest = new QualityGateCheck(wsClient, analysisMode, reportMetadataHolder, properties); - - @Before - public void before() { +@ExtendWith(MockitoExtension.class) +class QualityGateCheckTest { + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private DefaultScannerWsClient wsClient; + @Mock + private GlobalAnalysisMode analysisMode; + @Mock + private CeTaskReportDataHolder reportMetadataHolder; + @Mock + private ScanProperties properties; + + @RegisterExtension + private final LogTesterJUnit5 logTester = new LogTesterJUnit5(); + + private QualityGateCheck underTest; + + @BeforeEach + void before() { + underTest = new QualityGateCheck(wsClient, analysisMode, reportMetadataHolder, properties); logTester.setLevel(Level.DEBUG); - when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234"); - when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com"); } @Test - public void should_pass_if_quality_gate_ok() { + void should_pass_if_quality_gate_ok() { + when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234"); + when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com"); when(properties.shouldWaitForQualityGate()).thenReturn(true); when(properties.qualityGateWaitTimeout()).thenReturn(5); @@ -94,7 +100,9 @@ public class QualityGateCheckTest { } @Test - public void should_wait_and_then_pass_if_quality_gate_ok() { + void should_wait_and_then_pass_if_quality_gate_ok() { + when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234"); + when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com"); when(properties.shouldWaitForQualityGate()).thenReturn(true); when(properties.qualityGateWaitTimeout()).thenReturn(10); @@ -114,7 +122,9 @@ public class QualityGateCheckTest { } @Test - public void should_fail_if_quality_gate_none() { + void should_fail_if_quality_gate_none() { + when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234"); + when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com"); when(properties.shouldWaitForQualityGate()).thenReturn(true); when(properties.qualityGateWaitTimeout()).thenReturn(5); @@ -132,7 +142,9 @@ public class QualityGateCheckTest { } @Test - public void should_fail_if_quality_gate_error() { + void should_fail_if_quality_gate_error() { + when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234"); + when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com"); when(properties.shouldWaitForQualityGate()).thenReturn(true); when(properties.qualityGateWaitTimeout()).thenReturn(5); @@ -150,7 +162,9 @@ public class QualityGateCheckTest { } @Test - public void should_wait_and_then_fail_if_quality_gate_error() { + void should_wait_and_then_fail_if_quality_gate_error() { + when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234"); + when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com"); when(properties.shouldWaitForQualityGate()).thenReturn(true); when(properties.qualityGateWaitTimeout()).thenReturn(10); @@ -169,7 +183,9 @@ public class QualityGateCheckTest { } @Test - public void should_fail_if_quality_gate_timeout_exceeded() { + void should_fail_if_quality_gate_timeout_exceeded() { + when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234"); + when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com"); when(properties.shouldWaitForQualityGate()).thenReturn(true); when(properties.qualityGateWaitTimeout()).thenReturn(1); @@ -184,7 +200,7 @@ public class QualityGateCheckTest { } @Test - public void should_fail_if_cant_call_ws_for_quality_gate() { + void should_fail_if_cant_call_ws_for_quality_gate() { when(properties.shouldWaitForQualityGate()).thenReturn(true); when(properties.qualityGateWaitTimeout()).thenReturn(5); @@ -201,7 +217,7 @@ public class QualityGateCheckTest { } @Test - public void should_fail_if_invalid_response_from_quality_gate_ws() { + void should_fail_if_invalid_response_from_quality_gate_ws() { when(properties.shouldWaitForQualityGate()).thenReturn(true); when(properties.qualityGateWaitTimeout()).thenReturn(5); @@ -221,7 +237,7 @@ public class QualityGateCheckTest { } @Test - public void should_fail_if_cant_call_ws_for_task() { + void should_fail_if_cant_call_ws_for_task() { when(properties.shouldWaitForQualityGate()).thenReturn(true); when(properties.qualityGateWaitTimeout()).thenReturn(5); @@ -235,7 +251,7 @@ public class QualityGateCheckTest { } @Test - public void should_fail_if_invalid_response_from_ws_task() { + void should_fail_if_invalid_response_from_ws_task() { when(properties.shouldWaitForQualityGate()).thenReturn(true); when(properties.qualityGateWaitTimeout()).thenReturn(5); @@ -252,9 +268,9 @@ public class QualityGateCheckTest { .hasMessage("Failed to parse response from ce-task-url"); } - @Test - @UseDataProvider("ceTaskNotSucceededStatuses") - public void should_fail_if_task_not_succeeded(TaskStatus taskStatus) { + @ParameterizedTest + @MethodSource("ceTaskNotSucceededStatuses") + void should_fail_if_task_not_succeeded(TaskStatus taskStatus) { when(properties.shouldWaitForQualityGate()).thenReturn(true); when(properties.qualityGateWaitTimeout()).thenReturn(5); @@ -282,7 +298,7 @@ public class QualityGateCheckTest { } @Test - public void should_skip_wait_if_disabled() { + void should_skip_wait_if_disabled() { when(properties.shouldWaitForQualityGate()).thenReturn(false); underTest.start(); @@ -294,7 +310,7 @@ public class QualityGateCheckTest { } @Test - public void should_fail_if_enabled_with_medium_test() { + void should_fail_if_enabled_with_medium_test() { when(properties.shouldWaitForQualityGate()).thenReturn(true); when(analysisMode.isMediumTest()).thenReturn(true); @@ -319,12 +335,11 @@ public class QualityGateCheckTest { return qualityGateWsResponse; } - @DataProvider - public static Object[][] ceTaskNotSucceededStatuses() { - return new Object[][] { - {TaskStatus.CANCELED}, - {TaskStatus.FAILED}, - }; + private static Stream<TaskStatus> ceTaskNotSucceededStatuses() { + return Stream.of( + TaskStatus.CANCELED, + TaskStatus.FAILED + ); } private static class WsRequestPathMatcher implements ArgumentMatcher<WsRequest> { |