Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #4478] Upgrade JUnit to JUnit Jupiter #4475

Merged
merged 8 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate testing to JUnit Jupiter
In its current form, the project has some JUnit 4 and some JUnit 5
(Jupiter) tests. This patch aims to align all the tests to a single
modern framework, JUnit Jupiter, in order to make future development
easier.
As this patch is already pretty large as-is, it attempts to be
non-opinionated and simply replace JUnit 4 calls with the closed JUnit
Jupiter equivalents. Subsequent work may want to change some tests to
take further advantage of JUnit Jupiter's features.

This patch includes the following changes:
1. Gradle dependencies:
   a. All the dependencies under org.junit.jupiter were consolidated
      to use the single artifact org.junit.jupiter:junit-jupiter.
   b. The junit:junit dependency was removed in favor of
      org.junit.jupiter:junit-jupiter as mentioned in 1.a..
   c. The org.mockito:mockito-junit-jupiter dependency was added to
      provide the integration with Mockito.
   d. The com.github.stefanbirkner:system-rules dependency was
      removed in favor of org.junit-pioneer:junit-pioneer that was
      used to provide the same functionality as mentioned in 2.i.

2. Annotations
   a. org.junit.jupiter.api.BeforeEach was used as a drop-in
      replacement for org.junit.Before.
   b. org.junit.jupiter.api.BeforeAll was used as a drop-in
      replacement for org.junit.BeforeClass.
   c. org.junit.jupiter.api.AfterEach was used as a drop-in
      replacement for org.junit.After.
   d. org.junit.jupiter.api.AfterAll was used as a drop-in
      replacement for org.junit.AfterClass.
   e. org.junit.jupiter.api.AfterEach was used as a drop-in
      replacement for org.junit.After.
   f. org.junit.jupiter.api.Disabled was used as a drop-in
      replacement for org.junit.Ignore.
   g. org.junit.jupiter.api.Test was used as a replacement for
      org.junit.Test, although with some caveats:
      1. For the simple case with no arguments,
         org.junit.jupiter.api.Test was used as a drop-in replacement
         for org.junit.Test.
      2. For the case where org.junit.Test was used with a timeout
         argument, a combination of org.junit.jupiter.api.Test and
         org.junit.jupiter.api.Timeout was used.
      3. For the case where org.junit.Test was used with an expected
         argument, org.junit.jupiter.api.Test was used, but the
         assertion on the exception begin thrown is done explicitly in
         the test's code, see 3.e. below.
   h. org.junit.jupiter.api.extension.ExtendWith was used as a
      replacement for org.junit.runner.RunWith with an extension
      corresponding to the runner being replaced.
      a. org.mockito.junit.jupiter.MockitoExtension was used to
         provide the same functionality as
         org.mockito.junit.MockitoJUnitRunner. Since the extension is
         stricter than the runner, in some cases
         org.mockito.junit.jupiter.MockitoSettings was used to
         explicitly make the mocking more lenient.
   i. org.junitpioneer.jupiter.SetEnvironmentVariable was used in
      order to set environment variables in the tests instead of
      explicitly calling
      org.junit.contrib.java.lang.system.EnvironmentVariables in the
      test's body. As an added bonus, using this annotation also
      cleans up the changes to the environment variables when the test
      is over and prevents it from inadvertently effecting other
      tests.

3. Assertions
   a. org.junit.jupiter.api.Assertions was used as a drop-in
      replacement for org.junit.Assert for the case where the
      assertion was performed without a message.
   b. org.junit.jupiter.api.Assertions was used instead of
      org.junit.Assert in the cases where an assertion method was used
      with a message, but the argument were permuted to fit the new
      method's signature.
   c. org.junit.jupiter.api.Assertions does not have an equivalent of
      org.junit.Assert's assertThat method, but luckily it was only
      used in a few places, and always used the
      org.hamcrest.CoreMatchers.is matcher. These assertions were
      rewritten as straight-forward assertEquals assertions.
   c. org.junit.jupiter.api.Assertions does not have an equivalent of
      org.junit.Assert's assertThat method, but luckily it was only
      used in a few places, and always used the
      org.hamcrest.CoreMatchers.is matcher. These assertions were
      rewritten as straight-forward assertEquals assertions.
   d. org.junit.jupiter.api.Assertions does not have an equivalent of
      org.hamcrest.MatcherAssert's assertThat method, but luckily it
      was only used in a few places, and always used the
      org.hamcrest.CoreMatchers.is matcher. These assertions were
      rewritten as straight-forward assertEquals assertions.
   e. org.junit.jupiter.api.Assertions' assertThrows was used to
      assert an expected exception is throws instead of using
      org.junit.Test with the expected annotation.
  • Loading branch information
mureinik committed Oct 9, 2023
commit d056f4780ccd9cf6175ebcb5cb659fb03221fa9e
11 changes: 8 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ allprojects {
url "https://maven.aliyun.com/repository/public"
}
}
testImplementation "junit:junit:4.13.2"
testImplementation "org.junit.jupiter:junit-jupiter:5.6.0"
}

spotless {
Expand All @@ -126,6 +126,10 @@ allprojects {
}
}
spotlessJava.dependsOn(compileJava, javadoc, compileTestJava, test, processResources, processTestResources)

test {
useJUnitPlatform()
}
}

task tar(type: Tar) {
Expand Down Expand Up @@ -516,12 +520,13 @@ subprojects {
dependency "org.springframework.boot:spring-boot-starter-web:2.7.10"
dependency "io.openmessaging:registry-server:0.0.1"

dependency "junit:junit:4.13.2"
dependency "com.github.stefanbirkner:system-rules:1.16.1"
dependency "org.junit.jupiter:junit-jupiter:5.6.0"
dependency "org.junit-pioneer:junit-pioneer:1.9.1"
dependency "org.assertj:assertj-core:2.6.0"

dependency "org.mockito:mockito-core:3.8.0"
dependency "org.mockito:mockito-inline:3.8.0"
dependency "org.mockito:mockito-junit-jupiter:3.8.0"

dependency "io.cloudevents:cloudevents-core:2.4.2"
dependency "io.cloudevents:cloudevents-json-jackson:2.4.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

package org.apache.eventmesh.admin.rocketmq.request;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package org.apache.eventmesh.admin.rocketmq.response;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.eventmesh.admin.rocketmq.util;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -26,7 +26,7 @@
import java.net.URI;
import java.net.URISyntaxException;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import com.sun.net.httpserver.HttpExchange;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package org.apache.eventmesh.admin.rocketmq.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -29,16 +29,16 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class UrlMappingPatternTest {

private static final String TEST_URL_MAPPING_PATTERN = "/test/{param1}/path/{param2}";

private TestUrlMappingPattern urlMappingPattern;

@Before
@BeforeEach
public void setUp() {
urlMappingPattern = new TestUrlMappingPattern(TEST_URL_MAPPING_PATTERN);
}
Expand Down
3 changes: 2 additions & 1 deletion eventmesh-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dependencies {
implementation "io.grpc:grpc-stub:${grpcVersion}"
implementation "javax.annotation:javax.annotation-api:1.3.2"

implementation "com.github.stefanbirkner:system-rules"
testImplementation "org.junit-pioneer:junit-pioneer"
implementation "org.yaml:snakeyaml"

compileOnly 'org.projectlombok:lombok'
Expand Down Expand Up @@ -77,4 +77,5 @@ dependencies {
testImplementation "org.assertj:assertj-core"

testImplementation "org.mockito:mockito-core"
testImplementation "org.mockito:mockito-junit-jupiter"
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class EventMeshMessageTest {

@Test
public void testGetProp() {
EventMeshMessage message = createLiteMessage();
Assert.assertEquals(2L, message.getProp().size());
Assertions.assertEquals(2L, message.getProp().size());
}

@Test
Expand All @@ -37,30 +37,30 @@ public void testSetProp() {
Map<String, String> prop = new HashMap<>();
prop.put("key3", "value3");
message.setProp(prop);
Assert.assertEquals(1L, message.getProp().size());
Assert.assertEquals("value3", message.getProp("key3"));
Assertions.assertEquals(1L, message.getProp().size());
Assertions.assertEquals("value3", message.getProp("key3"));
}

@Test
public void testAddProp() {
EventMeshMessage message = createLiteMessage();
message.addProp("key3", "value3");
Assert.assertEquals(3L, message.getProp().size());
Assert.assertEquals("value1", message.getProp("key1"));
Assertions.assertEquals(3L, message.getProp().size());
Assertions.assertEquals("value1", message.getProp("key1"));
}

@Test
public void testGetPropKey() {
EventMeshMessage message = createLiteMessage();
Assert.assertEquals("value1", message.getProp("key1"));
Assertions.assertEquals("value1", message.getProp("key1"));
}

@Test
public void testRemoveProp() {
EventMeshMessage message = createLiteMessage();
message.removePropIfPresent("key1");
Assert.assertEquals(1L, message.getProp().size());
Assert.assertNull(message.getProp("key1"));
Assertions.assertEquals(1L, message.getProp().size());
Assertions.assertNull(message.getProp("key1"));
}

private EventMeshMessage createLiteMessage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@

package org.apache.eventmesh.common;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class EventMeshThreadFactoryTest {

@Test
public void testGetThreadNamePrefix() {
final String threadNamePrefix = "threadNamePrefix";
EventMeshThreadFactory factory = new EventMeshThreadFactory(threadNamePrefix, false);
Assert.assertEquals(threadNamePrefix, factory.getThreadNamePrefix());
Assertions.assertEquals(threadNamePrefix, factory.getThreadNamePrefix());
}

@Test
Expand All @@ -36,8 +36,8 @@ public void testNewThread() {
Thread t = factory.newThread(() -> {

});
Assert.assertNotNull(t);
Assert.assertEquals(threadNamePrefix + "-1", t.getName());
Assert.assertTrue(t.isDaemon());
Assertions.assertNotNull(t);
Assertions.assertEquals(threadNamePrefix + "-1", t.getName());
Assertions.assertTrue(t.isDaemon());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

public class ResetCountDownLatchTest {

Expand All @@ -29,51 +30,52 @@ public void testConstructorParameterError() {
try {
new ResetCountDownLatch(-1);
} catch (IllegalArgumentException e) {
Assert.assertEquals(e.getMessage(), "count must be greater than or equal to 0");
Assertions.assertEquals(e.getMessage(), "count must be greater than or equal to 0");
}
ResetCountDownLatch resetCountDownLatch = new ResetCountDownLatch(1);
Assert.assertEquals(1, resetCountDownLatch.getCount());
Assertions.assertEquals(1, resetCountDownLatch.getCount());
}

@Test
public void testAwaitTimeout() throws InterruptedException {
ResetCountDownLatch latch = new ResetCountDownLatch(1);
boolean await = latch.await(5, TimeUnit.MILLISECONDS);
Assert.assertFalse(await);
Assertions.assertFalse(await);
latch.countDown();
await = latch.await(5, TimeUnit.MILLISECONDS);
Assert.assertTrue(await);
Assertions.assertTrue(await);
}

@Test(timeout = 1000)
@Test
@Timeout(1000)
public void testCountDownAndGetCount() throws InterruptedException {
int count = 2;
ResetCountDownLatch resetCountDownLatch = new ResetCountDownLatch(count);
Assert.assertEquals(count, resetCountDownLatch.getCount());
Assertions.assertEquals(count, resetCountDownLatch.getCount());
resetCountDownLatch.countDown();
Assert.assertEquals(count - 1, resetCountDownLatch.getCount());
Assertions.assertEquals(count - 1, resetCountDownLatch.getCount());
resetCountDownLatch.countDown();
resetCountDownLatch.await();
Assert.assertEquals(0, resetCountDownLatch.getCount());
Assertions.assertEquals(0, resetCountDownLatch.getCount());
}

@Test
public void testReset() throws InterruptedException {
int count = 2;
ResetCountDownLatch resetCountDownLatch = new ResetCountDownLatch(count);
resetCountDownLatch.countDown();
Assert.assertEquals(count - 1, resetCountDownLatch.getCount());
Assertions.assertEquals(count - 1, resetCountDownLatch.getCount());
resetCountDownLatch.reset();
Assert.assertEquals(count, resetCountDownLatch.getCount());
Assertions.assertEquals(count, resetCountDownLatch.getCount());
resetCountDownLatch.countDown();
resetCountDownLatch.countDown();
resetCountDownLatch.await();
Assert.assertEquals(0, resetCountDownLatch.getCount());
Assertions.assertEquals(0, resetCountDownLatch.getCount());
resetCountDownLatch.countDown();
Assert.assertEquals(0, resetCountDownLatch.getCount());
Assertions.assertEquals(0, resetCountDownLatch.getCount());
resetCountDownLatch.reset();
resetCountDownLatch.countDown();
Assert.assertEquals(1, resetCountDownLatch.getCount());
Assertions.assertEquals(1, resetCountDownLatch.getCount());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,37 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

public class ThreadWrapperTest {

@Test
public void getThreadName() {
ThreadWrapper wrapper = createThreadWrapper(false);
wrapper.start();
Assert.assertEquals("EventMesh-Wrapper-mxsm", wrapper.thread.getName());
Assertions.assertEquals("EventMesh-Wrapper-mxsm", wrapper.thread.getName());
}

@Test
public void start() {
ThreadWrapper wrapper = createThreadWrapper(false);
wrapper.start();
Assert.assertTrue(wrapper.isStated());
Assertions.assertTrue(wrapper.isStated());
}

@Test(timeout = 1000)
@Test
@Timeout(1000)
public void await() {
ThreadWrapper wrapper = createThreadWrapper(false);
wrapper.start();
wrapper.await(1, TimeUnit.MILLISECONDS);
Assert.assertFalse(wrapper.hasWakeup.get());
Assertions.assertFalse(wrapper.hasWakeup.get());
wrapper.wakeup();
Assert.assertTrue(wrapper.hasWakeup.get());
Assertions.assertTrue(wrapper.hasWakeup.get());
wrapper.await();
Assert.assertFalse(wrapper.hasWakeup.get());
Assertions.assertFalse(wrapper.hasWakeup.get());
wrapper.await(2, TimeUnit.MILLISECONDS);

}
Expand Down Expand Up @@ -79,7 +81,7 @@ public void run() {
};
wrapper.start();
wrapper.shutdown();
Assert.assertEquals(100, counter.get());
Assertions.assertEquals(100, counter.get());
}

@Test
Expand All @@ -104,18 +106,18 @@ public void run() {
};
wrapper.start();
wrapper.shutdownImmediately();
Assert.assertEquals(0, counter.get());
Assertions.assertEquals(0, counter.get());
}

@Test
public void setDaemon() {
ThreadWrapper threadWrapper = createThreadWrapper(true);
threadWrapper.start();
Assert.assertTrue(threadWrapper.thread.isDaemon());
Assertions.assertTrue(threadWrapper.thread.isDaemon());

ThreadWrapper threadWrapper1 = createThreadWrapper(false);
threadWrapper1.start();
Assert.assertFalse(threadWrapper1.thread.isDaemon());
Assertions.assertFalse(threadWrapper1.thread.isDaemon());
}

private ThreadWrapper createThreadWrapper(boolean daemon) {
Expand Down
Loading