Skip to content

Commit

Permalink
Merge pull request #24 from adhie1337/add-more-tests
Browse files Browse the repository at this point in the history
chore: add more tests
  • Loading branch information
giurim authored Jun 4, 2020
2 parents f2f12b6 + 6d64524 commit 6c0e851
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/test/java/co/helmethair/scalatest/AsyncFunSpecTest.java
Original file line number Diff line number Diff line change
@@ -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<String> calls = Arrays.asList(
"setup",
"runs asynchronously",
"runs again asynchronously",
"cleanup"
);

verifyTestExecuteCode(calls, () -> engine.execute(executionRequest));
}
}
27 changes: 27 additions & 0 deletions src/test/java/co/helmethair/scalatest/OneInstancePerTestTest.java
Original file line number Diff line number Diff line change
@@ -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<String> calls = Arrays.asList("2", "3");

verifyTestExecuteCode(calls, () -> engine.execute(executionRequest));
}
}
11 changes: 11 additions & 0 deletions src/test/java/co/helmethair/scalatest/helper/RegisterCall.java
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -26,6 +28,15 @@ static void verifyTestExecuteCode(Map<String, Integer> callers, Body body) {
}
}

static void verifyTestExecuteCode(List<String> 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();
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/co/helmethair/scalatest/helper/TestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ default void verifyTestExecuteCode(Map<String, Integer> calls, RegisterCall.Body
RegisterCall.verifyTestExecuteCode(calls, body);
}

default void verifyTestExecuteCode(List<String> calls, RegisterCall.Body body) {
RegisterCall.verifyTestExecuteCode(calls, body);
}

default void verifyTestStartReported(String testIdSuffix, TestEngineExecutionListener listener) {
verifyTestStartReported(testIdSuffix, listener, atLeastOnce());
}
Expand Down
37 changes: 37 additions & 0 deletions src/test/scala/tests/AsyncFunSpecTest.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
29 changes: 29 additions & 0 deletions src/test/scala/tests/OneInstancePerTestTest.scala
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 6c0e851

Please sign in to comment.