Skip to content

Commit 368873f

Browse files
LarsEckartisidorejlink
committed
F!! removed junit5 runtime dependency
Co-Authored-By: Llewellyn Falco <[email protected]> Co-Authored-By: Johannes Link <[email protected]>
1 parent 01be1e0 commit 368873f

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.approvaltests.integrations.junit5;
2+
3+
import com.spun.util.tests.TestUtils;
4+
import org.approvaltests.namer.AttributeStackSelector;
5+
import org.junit.platform.commons.annotation.Testable;
6+
import org.junit.platform.commons.util.AnnotationUtils;
7+
8+
import java.lang.reflect.Method;
9+
import java.util.List;
10+
11+
public class JUnitUtils
12+
{
13+
public static boolean isTestableMethodForJunit(StackTraceElement element)
14+
{
15+
String fullClassName = element.getClassName();
16+
Class<?> clazz = AttributeStackSelector.loadClass(fullClassName);
17+
String methodName = TestUtils.unrollLambda(element.getMethodName());
18+
List<Method> methods = AttributeStackSelector.getMethodsByName(clazz, methodName);
19+
if (methods.isEmpty())
20+
{ return false; }
21+
for (Method method : methods)
22+
{
23+
// TODO: TestFactory might needs special attention
24+
if (AnnotationUtils.isAnnotated(method, Testable.class))
25+
{ return true; }
26+
}
27+
return false;
28+
}
29+
}

approvaltests/src/main/java/org/approvaltests/integrations/junit5/JUnit5Approvals.java renamed to approvaltests/src/main/java/org/approvaltests/integrations/junit5/JupiterApprovals.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import org.junit.jupiter.api.DynamicTest;
77
import org.lambda.actions.Action0;
88

9-
public class JUnit5Approvals
9+
public class JupiterApprovals
1010
{
1111
@Experimental
1212
public static DynamicTest dynamicTest(String displayName, Action0 action0)

approvaltests/src/main/java/org/approvaltests/namer/AttributeStackSelector.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import com.spun.util.ObjectUtils;
55
import com.spun.util.io.StackElementSelector;
66
import com.spun.util.tests.TestUtils;
7-
import org.junit.platform.commons.annotation.Testable;
8-
import org.junit.platform.commons.util.AnnotationUtils;
7+
import org.approvaltests.integrations.junit5.JUnitUtils;
98

109
import java.lang.annotation.Annotation;
1110
import java.lang.reflect.Method;
@@ -40,7 +39,7 @@ private List<Class<? extends Annotation>> getAvailableAttributes()
4039
}
4140
return attributes;
4241
}
43-
private static Class<? extends Annotation> loadClass(String className)
42+
public static Class<? extends Annotation> loadClass(String className)
4443
{
4544
Class<? extends Annotation> clazz = null;
4645
try
@@ -81,21 +80,26 @@ private boolean isTestCase(StackTraceElement element)
8180
{ return true; }
8281
return isTestAttribute(clazz, TestUtils.unrollLambda(element.getMethodName()));
8382
}
83+
public static Boolean isJunit5Present = null;
8484
public static boolean isTestableMethod(StackTraceElement element)
8585
{
86-
String fullClassName = element.getClassName();
87-
Class<?> clazz = loadClass(fullClassName);
88-
String methodName = TestUtils.unrollLambda(element.getMethodName());
89-
List<Method> methods = getMethodsByName(clazz, methodName);
90-
if (methods.isEmpty())
91-
{ return false; }
92-
for (Method method : methods)
86+
if (isJunit5Present == null)
9387
{
94-
if (AnnotationUtils.isAnnotated(method, Testable.class))
95-
{ return true; }
88+
try
89+
{
90+
Class.forName("org.junit.platform.commons.annotation.Testable");
91+
isJunit5Present = true;
92+
}
93+
catch (ClassNotFoundException e)
94+
{
95+
isJunit5Present = false;
96+
}
9697
}
97-
return false;
98+
if (!isJunit5Present)
99+
{ return false; }
100+
return JUnitUtils.isTestableMethodForJunit(element);
98101
}
102+
99103
private boolean isJunit3Test(Class<?> clazz)
100104
{
101105
Class<?> testcase = loadClass("junit.framework.TestCase");

approvaltests/src/test/java/org/approvaltests/namer/JUnit5StackTraceNamerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.spun.util.tests.TestUtils;
66
import com.spun.util.tests.TestUtils.SourceDirectoryRestorer;
77
import org.approvaltests.Approvals;
8-
import org.approvaltests.integrations.junit5.JUnit5Approvals;
8+
import org.approvaltests.integrations.junit5.JupiterApprovals;
99
import org.junit.jupiter.api.Assertions;
1010
import org.junit.jupiter.api.DisplayName;
1111
import org.junit.jupiter.api.DynamicTest;
@@ -93,13 +93,13 @@ void testOverridingDirectoryFinder()
9393
Collection<DynamicTest> testFactory()
9494
{
9595
return Arrays.asList(
96-
JUnit5Approvals.dynamicTest("test 1",
96+
JupiterApprovals.dynamicTest("test 1",
9797
() -> StackTraceNamerUtils.assertNamerForFramework(this.getClass().getSimpleName(),
9898
"testFactory.test_1")),
99-
JUnit5Approvals.dynamicTest("test 3",
99+
JupiterApprovals.dynamicTest("test 3",
100100
() -> StackTraceNamerUtils.assertNamerForFramework(this.getClass().getSimpleName(),
101101
"testFactory.test_3")),
102-
JUnit5Approvals.dynamicTest("test 2", () -> StackTraceNamerUtils
102+
JupiterApprovals.dynamicTest("test 2", () -> StackTraceNamerUtils
103103
.assertNamerForFramework(this.getClass().getSimpleName(), "testFactory.test_2")));
104104
}
105105
@TestFactory

0 commit comments

Comments
 (0)