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 5 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
34 changes: 32 additions & 2 deletions build.sbt
Expand Up @@ -188,15 +188,43 @@ 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(
libraryDependencies ++= Seq(junit4, junit4Interface),
libraryDependencies --= Seq(junit5, junit5Interface),
(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(
(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 +499,8 @@ lazy val userProjects = Seq[ProjectReference](
PlayOpenIdProject,
PlaySpecs2Project,
PlayTestProject,
PlayTestJUnit4Project,
PlayTestJUnit5Project,
PlayExceptionsProject,
PlayFiltersHelpersProject,
StreamsProject,
Expand Down
Expand Up @@ -4,14 +4,14 @@

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 {
Expand All @@ -37,7 +37,7 @@ public void getAll_shouldReturnAllValuesWithTheGivenKeys() throws Exception {
expectedMap.put(key1, value1);
expectedMap.put(key2, value2);

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

@Test
Expand All @@ -59,7 +59,7 @@ public void getAll_shouldCreateTheMissingValuesAndReturnAllWithTheGivenKeys() th
expectedMap.put(key1, value1);
expectedMap.put(key2, value2);

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

@Test
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 {
public 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());
}
}
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,7 +14,7 @@
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;
Expand All @@ -33,8 +30,8 @@ public void addBindings() {
.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
Expand All @@ -56,16 +53,16 @@ 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() {
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
Expand All @@ -76,7 +73,7 @@ public void setInitialConfigurationLoader() {
.withConfigLoader(env -> extra.withFallback(ConfigFactory.load(env.classLoader())))
.build();

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

@Test
Expand All @@ -93,7 +90,7 @@ 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
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
Expand Up @@ -4,16 +4,14 @@

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;
Expand All @@ -31,8 +29,8 @@ public void additionalModulesAndBindings() {
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
Expand All @@ -50,7 +48,7 @@ public GuiceApplicationBuilder builder(Context context) {
};
Application app = loader.load(fakeContext());

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

@Test
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