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

[pekko-testkit-typed] Support for JUnit5 in testkit #751

Merged
merged 13 commits into from
Nov 10, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.pekko.actor.testkit.typed.annotations;

import java.lang.annotation.*;

@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface JUnit5TestKit {}
thmue marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.pekko.actor.testkit.typed.javadsl

import org.apache.pekko
import com.typesafe.config.Config
import pekko.actor.testkit.typed.internal.TestKitUtils
import pekko.actor.testkit.typed.scaladsl.ActorTestKit.ApplicationTestConfig
import pekko.actor.typed.ActorSystem

final class Junit5TestKitBuilder() {

var system: Option[ActorSystem[_]] = None

var customConfig: Config = ApplicationTestConfig

var name: String = TestKitUtils.testNameFromCallStack(classOf[Junit5TestKitBuilder])

def withSystem(system: ActorSystem[_]): Junit5TestKitBuilder = {
this.system = Some(system)
this
}

def withCustomConfig(customConfig: Config): Junit5TestKitBuilder = {
this.customConfig = customConfig
this
}

def withName(name: String): Junit5TestKitBuilder = {
this.name = name
this
}

def build(): ActorTestKit = {
if (system.isDefined) {
return ActorTestKit.create(system.get)
}
ActorTestKit.create(name, customConfig)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* license agreements; and to You under the Apache License, version 2.0:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* This file is part of the Apache Pekko project, which was derived from Akka.
*/

package org.apache.pekko.actor.testkit.typed.javadsl

import org.junit.jupiter.api.extension.InvocationInterceptor.Invocation
import org.junit.jupiter.api.extension.{ ExtensionContext, InvocationInterceptor, ReflectiveInvocationContext }
import org.slf4j.LoggerFactory
import org.apache.pekko.actor.testkit.typed.internal.CapturingAppender

import java.lang.reflect.Method
import scala.util.control.NonFatal

final class LogCapturingExtension extends InvocationInterceptor {

private val capturingAppender = CapturingAppender.get("")

private val myLogger = LoggerFactory.getLogger(classOf[LogCapturing])

@throws[Throwable]
override def interceptTestMethod(invocation: Invocation[Void], invocationContext: ReflectiveInvocationContext[Method],
extensionContext: ExtensionContext): Unit = {

val testClassName = invocationContext.getTargetClass.getSimpleName
val testMethodName = invocationContext.getExecutable.getName

try {
myLogger.info(s"Logging started for test [${testClassName}: ${testMethodName}]")
invocation.proceed
myLogger.info(
s"Logging finished for test [${testClassName}: ${testMethodName}] that was successful")
} catch {
case NonFatal(e) =>
println(
s"--> [${Console.BLUE}${testClassName}: ${testMethodName}${Console.RESET}] " +
s"Start of log messages of test that failed with ${e.getMessage}")
capturingAppender.flush()
println(
s"<-- [${Console.BLUE}${testClassName}: ${testMethodName}${Console.RESET}] " +
s"End of log messages of test that failed with ${e.getMessage}")
throw e
} finally {

capturingAppender.clear()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.pekko.actor.testkit.typed.javadsl

import org.apache.pekko
import pekko.actor.testkit.typed.annotations.JUnit5TestKit
import org.junit.jupiter.api.extension.{ AfterAllCallback, BeforeTestExecutionCallback, ExtensionContext }
import org.junit.platform.commons.support.AnnotationSupport

final class TestKitJunit5Extension() extends AfterAllCallback with BeforeTestExecutionCallback {

var testKit: Option[ActorTestKit] = None

/**
* Get a reference to the field annotated with @Junit5Testkit [[Junit5TestKit]]
*/
override def beforeTestExecution(context: ExtensionContext): Unit = {

context.getTestInstance.ifPresent((instance: AnyRef) => {
val annotations = AnnotationSupport.findAnnotatedFieldValues(instance, classOf[JUnit5TestKit])
val fieldValue = annotations.stream().findFirst().orElseThrow(() =>
throw new IllegalArgumentException("Could not find field annotated with @Junit5TestKit"))
testKit = Some(fieldValue.asInstanceOf[ActorTestKit])
})
}

/**
* Shutdown testKit
*/
override def afterAll(context: ExtensionContext): Unit = {
testKit.get.shutdownTestKit()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* license agreements; and to You under the Apache License, version 2.0:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* This file is part of the Apache Pekko project, which was derived from Akka.
*/

package jdocs.org.apache.pekko.actor.testkit.typed.javadsl;

import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit;
import org.apache.pekko.actor.Address;
import org.apache.pekko.actor.testkit.typed.javadsl.*;
import org.apache.pekko.actor.testkit.typed.javadsl.Junit5TestKitBuilder;
import org.apache.pekko.actor.typed.ActorRef;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

// #junit5-integration
@DisplayName("JUnit5")
@ExtendWith(TestKitJunit5Extension.class)
class Junit5IntegrationExampleTest {

@JUnit5TestKit public ActorTestKit testKit = new Junit5TestKitBuilder().build();

@Test
void junit5Test() {
Address address = testKit.system().address();
assertNotNull(address);
}

@Test
void testSomething() {

ActorRef<AsyncTestingExampleTest.Echo.Ping> pinger =
testKit.spawn(AsyncTestingExampleTest.Echo.create(), "ping");
TestProbe<AsyncTestingExampleTest.Echo.Pong> probe = testKit.createTestProbe();
pinger.tell(new AsyncTestingExampleTest.Echo.Ping("hello", probe.ref()));
AsyncTestingExampleTest.Echo.Pong pong =
probe.expectMessage(new AsyncTestingExampleTest.Echo.Pong("hello"));
assertEquals("hello", pong.message);
}

@Test
void testSomething2() {
ActorRef<AsyncTestingExampleTest.Echo.Ping> pinger2 =
testKit.spawn(AsyncTestingExampleTest.Echo.create(), "ping2");
TestProbe<AsyncTestingExampleTest.Echo.Pong> probe2 = testKit.createTestProbe();
pinger2.tell(new AsyncTestingExampleTest.Echo.Ping("hello", probe2.ref()));
AsyncTestingExampleTest.Echo.Pong pong =
probe2.expectMessage(new AsyncTestingExampleTest.Echo.Pong("hello"));
assertEquals("hello", pong.message);
}
}
// #junit5-integration
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package jdocs.org.apache.pekko.actor.testkit.typed.javadsl;

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* license agreements; and to You under the Apache License, version 2.0:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* This file is part of the Apache Pekko project, which was derived from Akka.
*/

// #log-capturing-junit5

import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit;
import org.apache.pekko.actor.testkit.typed.javadsl.*;
import org.apache.pekko.actor.testkit.typed.javadsl.Junit5TestKitBuilder;
import org.apache.pekko.actor.typed.ActorRef;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static jdocs.org.apache.pekko.actor.testkit.typed.javadsl.AsyncTestingExampleTest.Echo;

// test code copied from LogCapturingExampleTest.java

@DisplayName("Junit5 log capturing")
@ExtendWith(TestKitJunit5Extension.class)
@ExtendWith(LogCapturingExtension.class)
class LogCapturingExtensionExampleTest {

@JUnit5TestKit public ActorTestKit testKit = new Junit5TestKitBuilder().build();

@Test
void testSomething() {
ActorRef<Echo.Ping> pinger = testKit.spawn(Echo.create(), "ping");
TestProbe<Echo.Pong> probe = testKit.createTestProbe();
pinger.tell(new Echo.Ping("hello", probe.ref()));
probe.expectMessage(new Echo.Pong("hello"));
}

@Test
void testSomething2() {
ActorRef<Echo.Ping> pinger = testKit.spawn(Echo.create(), "ping");
TestProbe<Echo.Pong> probe = testKit.createTestProbe();
pinger.tell(new Echo.Ping("hello", probe.ref()));
probe.expectMessage(new Echo.Pong("hello"));
}
}
// #log-capturing-junit5
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* license agreements; and to You under the Apache License, version 2.0:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* This file is part of the Apache Pekko project, which was derived from Akka.
*/

package org.apache.pekko.actor.testkit.typed.javadsl;

import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit;
import org.apache.pekko.Done;
import org.apache.pekko.actor.typed.javadsl.Behaviors;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.scalatestplus.junit.JUnitSuite;

import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import static org.apache.pekko.Done.done;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@DisplayName("ActorTestKitTestJunit5")
@ExtendWith(TestKitJunit5Extension.class)
@ExtendWith(LogCapturingExtension.class)
class ActorTestKitJunit5Test extends JUnitSuite {

@JUnit5TestKit public ActorTestKit testKit = new Junit5TestKitBuilder().build();

@Test
void systemNameShouldComeFromTestClassViaJunitResource() {
assertEquals("ActorTestKitJunit5Test", testKit.system().name());
}

@Test
void systemNameShouldComeFromTestClass() {
final ActorTestKit testKit2 = ActorTestKit.create();
try {
assertEquals("ActorTestKitJunit5Test", testKit2.system().name());
} finally {
testKit2.shutdownTestKit();
}
}

@Test
void systemNameShouldComeFromGivenClassName() {
final ActorTestKit testKit2 = ActorTestKit.create(HashMap.class.getName());
try {
// removing package name and such
assertEquals("HashMap", testKit2.system().name());
} finally {
testKit2.shutdownTestKit();
}
}

@Test
void testKitShouldSpawnActor() throws Exception {
final CompletableFuture<Done> started = new CompletableFuture<>();
testKit.spawn(
Behaviors.setup(
(context) -> {
started.complete(done());
return Behaviors.same();
}));
assertNotNull(started.get(3, TimeUnit.SECONDS));
}
}
1 change: 1 addition & 0 deletions actor-testkit-typed/src/test/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

# used by ActorTestKitSpec
test.from-application = yes
test.value = someValue
thmue marked this conversation as resolved.
Show resolved Hide resolved
Loading