diff options
author | Dejan Milisavljevic <130993898+dejan-milisavljevic-sonarsource@users.noreply.github.com> | 2024-02-26 10:27:42 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-02-26 20:02:36 +0000 |
commit | 990606438b5fb472915f244a5ea68386abb63c1d (patch) | |
tree | 6f5c0dcbc0781da0921ab8670f09c7d252644842 /server/sonar-process | |
parent | e2e4ca5594431f7ed596d7da93dfc21656c02ddb (diff) | |
download | sonarqube-990606438b5fb472915f244a5ea68386abb63c1d.tar.gz sonarqube-990606438b5fb472915f244a5ea68386abb63c1d.zip |
SONAR-21643 Make rule compatible with JUnit5 (#10700)
Diffstat (limited to 'server/sonar-process')
3 files changed, 35 insertions, 11 deletions
diff --git a/server/sonar-process/build.gradle b/server/sonar-process/build.gradle index 86cbfc591f7..63a03012d98 100644 --- a/server/sonar-process/build.gradle +++ b/server/sonar-process/build.gradle @@ -29,7 +29,16 @@ dependencies { testImplementation 'junit:junit' testImplementation 'org.assertj:assertj-core' testImplementation 'org.hamcrest:hamcrest-core' + testImplementation 'org.junit.jupiter:junit-jupiter-api' testImplementation 'org.mockito:mockito-core' testImplementation 'org.awaitility:awaitility' testImplementation project(':sonar-testing-harness') + + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' + testRuntimeOnly 'org.junit.vintage:junit-vintage-engine' +} + +test { + // Enabling the JUnit Platform (see https://github.com/junit-team/junit5-samples/tree/master/junit5-migration-gradle) + useJUnitPlatform() } diff --git a/server/sonar-process/src/test/java/org/sonar/process/LoggingRule.java b/server/sonar-process/src/test/java/org/sonar/process/LoggingRule.java index 3a3896d74b1..194120edaee 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/LoggingRule.java +++ b/server/sonar-process/src/test/java/org/sonar/process/LoggingRule.java @@ -23,12 +23,15 @@ import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.LoggingEvent; import ch.qos.logback.core.joran.spi.JoranException; import java.util.List; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.rules.ExternalResource; import org.slf4j.LoggerFactory; import org.slf4j.event.Level; import org.sonar.process.logging.LogbackHelper; -public class LoggingRule extends ExternalResource { +public class LoggingRule extends ExternalResource implements BeforeEachCallback, AfterEachCallback { private final Class loggerClass; @@ -37,13 +40,23 @@ public class LoggingRule extends ExternalResource { } @Override - protected void before() throws Throwable { + public void beforeEach(ExtensionContext extensionContext) throws Exception { + before(); + } + + @Override + protected void before() throws Exception { new LogbackHelper().resetFromXml("/org/sonar/process/logback-test.xml"); TestLogbackAppender.events.clear(); setLevel(Level.INFO); } @Override + public void afterEach(ExtensionContext extensionContext) throws Exception { + after(); + } + + @Override protected void after() { TestLogbackAppender.events.clear(); setLevel(Level.INFO); @@ -88,4 +101,5 @@ public class LoggingRule extends ExternalResource { .filter(e -> e.getLoggerName().equals(loggerClass.getName())) .anyMatch(e -> e.getFormattedMessage().equals(message)); } + } diff --git a/server/sonar-process/src/test/java/org/sonar/process/cluster/health/HealthStateRefresherTest.java b/server/sonar-process/src/test/java/org/sonar/process/cluster/health/HealthStateRefresherTest.java index d0d3e5577b4..8fb7d6ceba6 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/cluster/health/HealthStateRefresherTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/cluster/health/HealthStateRefresherTest.java @@ -22,8 +22,8 @@ package org.sonar.process.cluster.health; import com.hazelcast.core.HazelcastInstanceNotActiveException; import java.util.Random; import java.util.concurrent.TimeUnit; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; import org.mockito.ArgumentCaptor; import org.sonar.process.LoggingRule; @@ -39,9 +39,10 @@ import static org.mockito.Mockito.when; import static org.slf4j.event.Level.DEBUG; import static org.slf4j.event.Level.ERROR; -public class HealthStateRefresherTest { - @Rule - public final LoggingRule logging = new LoggingRule(HealthStateRefresher.class); +class HealthStateRefresherTest { + + @RegisterExtension + private final LoggingRule logging = new LoggingRule(HealthStateRefresher.class); private final Random random = new Random(); private final NodeDetailsTestSupport testSupport = new NodeDetailsTestSupport(random); @@ -52,7 +53,7 @@ public class HealthStateRefresherTest { private final HealthStateRefresher underTest = new HealthStateRefresher(executorService, nodeHealthProvider, sharedHealthState); @Test - public void start_adds_runnable_with_10_second_delay_and_initial_delay_putting_NodeHealth_from_provider_into_SharedHealthState() { + void start_adds_runnable_with_10_second_delay_and_initial_delay_putting_NodeHealth_from_provider_into_SharedHealthState() { ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class); NodeHealth[] nodeHealths = { testSupport.randomNodeHealth(), @@ -84,7 +85,7 @@ public class HealthStateRefresherTest { } @Test - public void stop_whenCalled_hasNoEffect() { + void stop_whenCalled_hasNoEffect() { underTest.stop(); verify(sharedHealthState).clearMine(); @@ -92,7 +93,7 @@ public class HealthStateRefresherTest { } @Test - public void stop_whenThrowHazelcastInactiveException_shouldSilenceError() { + void stop_whenThrowHazelcastInactiveException_shouldSilenceError() { logging.setLevel(DEBUG); SharedHealthState sharedHealthStateMock = mock(SharedHealthState.class); doThrow(HazelcastInstanceNotActiveException.class).when(sharedHealthStateMock).clearMine(); @@ -104,7 +105,7 @@ public class HealthStateRefresherTest { } @Test - public void start_whenHazelcastIsNotActive_shouldNotLogErrors() { + void start_whenHazelcastIsNotActive_shouldNotLogErrors() { logging.setLevel(DEBUG); doThrow(new HazelcastInstanceNotActiveException()).when(sharedHealthState).writeMine(any()); |