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

Migrate from JUnit 4 to JUnit 5 #11839

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 36 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,47 @@ lazy val PlayJpaProject = PlayCrossBuiltProject("Play-Java-JPA", "persistence/pl

lazy val PlayTestProject = PlayCrossBuiltProject("Play-Test", "testkit/play-test")
.settings(
libraryDependencies ++= testDependencies ++ Seq(h2database % "test"),
libraryDependencies ++= testDependencies ++ Seq(h2database % Test),
(Test / parallelExecution) := false
)
.dependsOn(
PlayGuiceProject,
PlayServerProject,
// We still need a server provider when running Play-Test tests.
// Since Akka HTTP is the default, we should use it here.
PlayAkkaHttpServerProject % "test"
PlayAkkaHttpServerProject % Test
)

lazy val PlayTestJUnit4Project = PlayCrossBuiltProject("Play-Test-JUnit4", "testkit/play-test-junit4")
.settings(
mimaPreviousArtifacts := Set.empty,
mimaFailOnNoPrevious := false,
libraryDependencies ++= Seq(junit4, junit4Interface),
(Test / parallelExecution) := false
)
.dependsOn(
PlayGuiceProject,
PlayServerProject,
// We still need a server provider when running Play-Test tests.
// Since Akka HTTP is the default, we should use it here.
PlayAkkaHttpServerProject % Test,
PlayTestProject
)

lazy val PlayTestJUnit5Project = PlayCrossBuiltProject("Play-Test-JUnit5", "testkit/play-test-junit5")
.settings(
libraryDependencies ++= Seq(junit5, junit5Interface),
mimaPreviousArtifacts := Set.empty,
mimaFailOnNoPrevious := false,
(Test / parallelExecution) := false,
)
.dependsOn(
PlayGuiceProject,
PlayServerProject,
// We still need a server provider when running Play-Test tests.
// Since Akka HTTP is the default, we should use it here.
PlayAkkaHttpServerProject % Test,
PlayTestProject
)

lazy val PlaySpecs2Project = PlayCrossBuiltProject("Play-Specs2", "testkit/play-specs2")
Expand Down Expand Up @@ -471,6 +503,8 @@ lazy val userProjects = Seq[ProjectReference](
PlayOpenIdProject,
PlaySpecs2Project,
PlayTestProject,
PlayTestJUnit4Project,
PlayTestJUnit5Project,
PlayExceptionsProject,
PlayFiltersHelpersProject,
StreamsProject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

package play.cache.caffeine;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

public class NamedCaffeineCacheSpec {
class NamedCaffeineCacheSpec {

private NamedCaffeineCache<String, String> cache =
new NamedCaffeineCache<>("testNamedCaffeineCache", Caffeine.newBuilder().buildAsync());

@Test
public void getAll_shouldReturnAllValuesWithTheGivenKeys() throws Exception {
void getAll_shouldReturnAllValuesWithTheGivenKeys() throws Exception {
String key1 = "key1";
String value1 = "value1";
String key2 = "key2";
Expand All @@ -37,11 +37,11 @@ public void getAll_shouldReturnAllValuesWithTheGivenKeys() throws Exception {
expectedMap.put(key1, value1);
expectedMap.put(key2, value2);

assertThat(resultMap, equalTo(expectedMap));
assertEquals(expectedMap, resultMap);
}

@Test
public void getAll_shouldCreateTheMissingValuesAndReturnAllWithTheGivenKeys() throws Exception {
void getAll_shouldCreateTheMissingValuesAndReturnAllWithTheGivenKeys() throws Exception {
String key1 = "key1";
String value1 = "value1";
String key2 = "key2";
Expand All @@ -59,11 +59,11 @@ public void getAll_shouldCreateTheMissingValuesAndReturnAllWithTheGivenKeys() th
expectedMap.put(key1, value1);
expectedMap.put(key2, value2);

assertThat(resultMap, equalTo(expectedMap));
assertEquals(expectedMap, resultMap);
}

@Test
public void getAll_shouldNotReplaceAlreadyExistingValues() throws Exception {
void getAll_shouldNotReplaceAlreadyExistingValues() throws Exception {
String key1 = "key1";
String value1 = "value1";
String key2 = "key2";
Expand All @@ -82,18 +82,18 @@ public void getAll_shouldNotReplaceAlreadyExistingValues() throws Exception {
expectedMap.put(key1, value1);
expectedMap.put(key2, value2);

assertThat(resultMap, equalTo(expectedMap));
assertEquals(expectedMap, resultMap);
}

@Test()
public void getAll_shouldReturnFailedFutureIfMappingFunctionIsCompletedExceptionally()
throws Exception {
void getAll_shouldReturnFailedFutureIfMappingFunctionIsCompletedExceptionally() {
LoggerFactory.getLogger(NamedCaffeineCache.class);
RuntimeException testException = new RuntimeException("test exception");
CompletableFuture<Map<String, String>> future = new CompletableFuture<>();
future.completeExceptionally(testException);
CompletableFuture<Map<String, String>> resultFuture =
cache.getAll(new HashSet<>(Arrays.asList("key1")), (missingKeys, executor) -> future);
assertThat(resultFuture.isCompletedExceptionally(), equalTo(true));
cache.getAll(new HashSet<>(List.of("key1")), (missingKeys, executor) -> future);

assertTrue(resultFuture.isCompletedExceptionally());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

package play.inject.guice;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.*;
import static play.inject.Bindings.bind;

import com.google.common.collect.ImmutableList;
Expand All @@ -17,28 +14,28 @@
import com.typesafe.config.ConfigFactory;
import javax.inject.Inject;
import javax.inject.Provider;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import play.Application;
import play.api.inject.guice.GuiceApplicationBuilderSpec;
import play.inject.Injector;
import play.libs.Scala;

public class GuiceApplicationBuilderTest {
class GuiceApplicationBuilderTest {

@Test
public void addBindings() {
void addBindings() {
Injector injector =
new GuiceApplicationBuilder()
.bindings(new AModule())
.bindings(bind(B.class).to(B1.class))
.injector();

assertThat(injector.instanceOf(A.class), instanceOf(A1.class));
assertThat(injector.instanceOf(B.class), instanceOf(B1.class));
assertInstanceOf(A1.class, injector.instanceOf(A.class));
assertInstanceOf(B1.class, injector.instanceOf(B.class));
}

@Test
public void overrideBindings() {
void overrideBindings() {
Application app =
new GuiceApplicationBuilder()
.bindings(new AModule())
Expand All @@ -56,31 +53,31 @@ public void overrideBindings() {
.injector()
.instanceOf(Application.class);

assertThat(app.config().getInt("a"), is(1));
assertThat(app.config().getInt("b"), is(2));
assertThat(app.injector().instanceOf(A.class), instanceOf(A2.class));
assertEquals(1, app.config().getInt("a"));
assertEquals(2, app.config().getInt("b"));
assertInstanceOf(A2.class, app.injector().instanceOf(A.class));
}

@Test
public void disableModules() {
void disableModules() {
Injector injector =
new GuiceApplicationBuilder().bindings(new AModule()).disable(AModule.class).injector();
assertThrows(ConfigurationException.class, () -> injector.instanceOf(A.class));
assertThrowsExactly(ConfigurationException.class, () -> injector.instanceOf(A.class));
}

@Test
public void setInitialConfigurationLoader() {
void setInitialConfigurationLoader() {
Config extra = ConfigFactory.parseMap(ImmutableMap.of("a", 1));
Application app =
new GuiceApplicationBuilder()
.withConfigLoader(env -> extra.withFallback(ConfigFactory.load(env.classLoader())))
.build();

assertThat(app.config().getInt("a"), is(1));
assertEquals(1, app.config().getInt("a"));
}

@Test
public void setModuleLoader() {
void setModuleLoader() {
Injector injector =
new GuiceApplicationBuilder()
.withModuleLoader(
Expand All @@ -93,11 +90,11 @@ public void setModuleLoader() {
Guiceable.bindings(bind(A.class).to(A1.class))))
.injector();

assertThat(injector.instanceOf(A.class), instanceOf(A1.class));
assertInstanceOf(A1.class, injector.instanceOf(A.class));
}

@Test
public void setLoadedModulesDirectly() {
void setLoadedModulesDirectly() {
Injector injector =
new GuiceApplicationBuilder()
.load(
Expand All @@ -108,7 +105,7 @@ public void setLoadedModulesDirectly() {
Guiceable.bindings(bind(A.class).to(A1.class)))
.injector();

assertThat(injector.instanceOf(A.class), instanceOf(A1.class));
assertInstanceOf(A1.class, injector.instanceOf(A.class));
}

public static interface A {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,37 @@

package play.inject.guice;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static play.inject.Bindings.bind;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import java.util.Properties;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import play.Application;
import play.ApplicationLoader;
import play.Environment;

public class GuiceApplicationLoaderTest {
class GuiceApplicationLoaderTest {

private ApplicationLoader.Context fakeContext() {
return ApplicationLoader.create(Environment.simple());
}

@Test
public void additionalModulesAndBindings() {
void additionalModulesAndBindings() {
GuiceApplicationBuilder builder =
new GuiceApplicationBuilder().bindings(new AModule()).bindings(bind(B.class).to(B1.class));
ApplicationLoader loader = new GuiceApplicationLoader(builder);
Application app = loader.load(fakeContext());

assertThat(app.injector().instanceOf(A.class), instanceOf(A1.class));
assertThat(app.injector().instanceOf(B.class), instanceOf(B1.class));
assertInstanceOf(A1.class, app.injector().instanceOf(A.class));
assertInstanceOf(B1.class, app.injector().instanceOf(B.class));
}

@Test
public void extendLoaderAndSetConfiguration() {
void extendLoaderAndSetConfiguration() {
ApplicationLoader loader =
new GuiceApplicationLoader() {
@Override
Expand All @@ -50,11 +48,11 @@ public GuiceApplicationBuilder builder(Context context) {
};
Application app = loader.load(fakeContext());

assertThat(app.config().getInt("a"), is(1));
assertEquals(1, app.config().getInt("a"));
}

@Test
public void usingAdditionalConfiguration() {
void usingAdditionalConfiguration() {
Properties properties = new Properties();
properties.setProperty("play.http.context", "/tests");

Expand All @@ -67,7 +65,7 @@ public void usingAdditionalConfiguration() {
ApplicationLoader.create(Environment.simple()).withConfig(config);
Application app = loader.load(context);

assertThat(app.asScala().httpConfiguration().context(), equalTo("/tests"));
assertEquals("/tests", app.asScala().httpConfiguration().context());
}

public interface A {}
Expand Down