diff --git a/src/test/java/co/helmethair/scalatest/AsyncFunSpecTest.java b/src/test/java/co/helmethair/scalatest/AsyncFunSpecTest.java new file mode 100644 index 0000000..ac2ac6a --- /dev/null +++ b/src/test/java/co/helmethair/scalatest/AsyncFunSpecTest.java @@ -0,0 +1,32 @@ +package co.helmethair.scalatest; + +import co.helmethair.scalatest.helper.TestEngineExecutionListener; +import co.helmethair.scalatest.helper.TestHelpers; +import org.junit.jupiter.api.Test; +import org.junit.platform.engine.EngineDiscoveryRequest; +import org.junit.platform.engine.ExecutionRequest; +import org.junit.platform.engine.TestDescriptor; + +import java.util.Arrays; +import java.util.List; + +import static org.mockito.Mockito.spy; + +public class AsyncFunSpecTest implements TestHelpers { + @Test + void areCalledAsynchronouslyTest() { + EngineDiscoveryRequest discoveryRequest = createClassDiscoveryRequest("tests.AsyncFunSpecTest"); + TestDescriptor discoveredTests = engine.discover(discoveryRequest, engineId); + TestEngineExecutionListener listener = spy(new TestEngineExecutionListener()); + ExecutionRequest executionRequest = new ExecutionRequest(discoveredTests, listener, null); + + List calls = Arrays.asList( + "setup", + "runs asynchronously", + "runs again asynchronously", + "cleanup" + ); + + verifyTestExecuteCode(calls, () -> engine.execute(executionRequest)); + } +} diff --git a/src/test/java/co/helmethair/scalatest/OneInstancePerTestTest.java b/src/test/java/co/helmethair/scalatest/OneInstancePerTestTest.java new file mode 100644 index 0000000..7bd5083 --- /dev/null +++ b/src/test/java/co/helmethair/scalatest/OneInstancePerTestTest.java @@ -0,0 +1,27 @@ +package co.helmethair.scalatest; + +import co.helmethair.scalatest.helper.TestEngineExecutionListener; +import co.helmethair.scalatest.helper.TestHelpers; +import org.junit.jupiter.api.Test; +import org.junit.platform.engine.EngineDiscoveryRequest; +import org.junit.platform.engine.ExecutionRequest; +import org.junit.platform.engine.TestDescriptor; + +import java.util.Arrays; +import java.util.List; + +import static org.mockito.Mockito.spy; + +public class OneInstancePerTestTest implements TestHelpers { + @Test + void areCalledFromOtherInstancesTest() { + EngineDiscoveryRequest discoveryRequest = createClassDiscoveryRequest("tests.OneInstancePerTestTest"); + TestDescriptor discoveredTests = engine.discover(discoveryRequest, engineId); + TestEngineExecutionListener listener = spy(new TestEngineExecutionListener()); + ExecutionRequest executionRequest = new ExecutionRequest(discoveredTests, listener, null); + + List calls = Arrays.asList("2", "3"); + + verifyTestExecuteCode(calls, () -> engine.execute(executionRequest)); + } +} diff --git a/src/test/java/co/helmethair/scalatest/helper/RegisterCall.java b/src/test/java/co/helmethair/scalatest/helper/RegisterCall.java index 9ea300c..050542e 100644 --- a/src/test/java/co/helmethair/scalatest/helper/RegisterCall.java +++ b/src/test/java/co/helmethair/scalatest/helper/RegisterCall.java @@ -1,7 +1,9 @@ package co.helmethair.scalatest.helper; +import org.mockito.InOrder; import org.mockito.Mockito; +import java.util.List; import java.util.Map; import static org.mockito.Mockito.*; @@ -26,6 +28,15 @@ static void verifyTestExecuteCode(Map callers, Body body) { } } + static void verifyTestExecuteCode(List callers, Body body) { + synchronized (mock) { + Mockito.reset(mock); + body.apply(); + InOrder inOrder = inOrder(mock); + callers.forEach(c -> inOrder.verify(mock).register(c)); + } + } + default void register() { synchronized (mock) { RegisterCall.mock.register(); diff --git a/src/test/java/co/helmethair/scalatest/helper/TestHelpers.java b/src/test/java/co/helmethair/scalatest/helper/TestHelpers.java index b5afabb..556f7e2 100644 --- a/src/test/java/co/helmethair/scalatest/helper/TestHelpers.java +++ b/src/test/java/co/helmethair/scalatest/helper/TestHelpers.java @@ -106,6 +106,10 @@ default void verifyTestExecuteCode(Map calls, RegisterCall.Body RegisterCall.verifyTestExecuteCode(calls, body); } + default void verifyTestExecuteCode(List calls, RegisterCall.Body body) { + RegisterCall.verifyTestExecuteCode(calls, body); + } + default void verifyTestStartReported(String testIdSuffix, TestEngineExecutionListener listener) { verifyTestStartReported(testIdSuffix, listener, atLeastOnce()); } diff --git a/src/test/scala/tests/AsyncFunSpecTest.scala b/src/test/scala/tests/AsyncFunSpecTest.scala new file mode 100644 index 0000000..0cb1235 --- /dev/null +++ b/src/test/scala/tests/AsyncFunSpecTest.scala @@ -0,0 +1,37 @@ +package tests + +import co.helmethair.scalatest.helper.RegisterCall +import org.scalatest.{ FutureOutcome, OneInstancePerTest } +import org.scalatest.funspec.AsyncFunSpec + +import scala.concurrent.Future + +class AsyncFunSpecTest extends AsyncFunSpec with RegisterCall with OneInstancePerTest { + + override def withFixture(test: NoArgAsyncTest): FutureOutcome = { + register("setup") + complete { + super.withFixture(test) // Invoke the test function + } lastly { + register("cleanup") + } + } + + describe("AsyncTestSuite") { + it("runs first") { + Future { + Thread.sleep(2000) + register("runs asynchronously") + assert(true) + } + } + + it("runs second") { + Future { + Thread.sleep(1000) + register("runs again asynchronously") + assert(true) + } + } + } +} diff --git a/src/test/scala/tests/OneInstancePerTestTest.scala b/src/test/scala/tests/OneInstancePerTestTest.scala new file mode 100644 index 0000000..6920c12 --- /dev/null +++ b/src/test/scala/tests/OneInstancePerTestTest.scala @@ -0,0 +1,29 @@ +package tests + +import co.helmethair.scalatest.helper.RegisterCall +import org.scalatest.OneInstancePerTest +import org.scalatest.funspec.AnyFunSpec + +class OneInstancePerTestTest extends AnyFunSpec with RegisterCall with OneInstancePerTest { + + private val id: Int = OneInstancePerTestTest.nextId + + describe("OneInstancePerTest") { + it("runs first") { + register(id.toString) + } + + it("runs second") { + register(id.toString) + } + } +} + +object OneInstancePerTestTest { + var id: Int = 0 + + def nextId: Int = { + id = id + 1 + id + } +}