You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2020/10/01 05:56:18 UTC
[groovy] branch master updated: GROOVY-9767: GroovyAssert should
not depend on org.junit.Assert
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 8cecad4 GROOVY-9767: GroovyAssert should not depend on org.junit.Assert
8cecad4 is described below
commit 8cecad4ce9805cd95ef02f5727c6a6b31352aaba
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu Oct 1 13:43:39 2020 +1000
GROOVY-9767: GroovyAssert should not depend on org.junit.Assert
---
.../src/main/java/groovy/test/GroovyAssert.java | 53 ++++++++++++++++++----
1 file changed, 43 insertions(+), 10 deletions(-)
diff --git a/subprojects/groovy-test/src/main/java/groovy/test/GroovyAssert.java b/subprojects/groovy-test/src/main/java/groovy/test/GroovyAssert.java
index 28e0840..de6b958 100644
--- a/subprojects/groovy-test/src/main/java/groovy/test/GroovyAssert.java
+++ b/subprojects/groovy-test/src/main/java/groovy/test/GroovyAssert.java
@@ -22,8 +22,8 @@ import groovy.lang.Closure;
import groovy.lang.GroovyRuntimeException;
import groovy.lang.GroovyShell;
import org.codehaus.groovy.runtime.ScriptBytecodeAdapter;
-import org.junit.Test;
+import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
@@ -33,30 +33,41 @@ import java.util.logging.Logger;
import static org.codehaus.groovy.runtime.DefaultGroovyMethods.isAtLeast;
/**
- * <p>{@code GroovyAssert} contains a set of static assertion and test helper methods and is supposed to be a Groovy
- * extension of JUnit 4's {@link org.junit.Assert} class. In case JUnit 3 is the choice, the {@link groovy.test.GroovyTestCase}
- * is meant to be used for writing tests based on {@link junit.framework.TestCase}.
+ * <p>{@code GroovyAssert} contains a set of static assertion and test helper methods for JUnit 4+.
+ * They augment the kind of helper methods found in JUnit 4's {@link org.junit.Assert} class.
+ * JUnit 3 users typically don't use these methods but instead,
+ * the equivalent methods in {@link groovy.test.GroovyTestCase}.
* </p>
*
* <p>
- * {@code GroovyAssert} methods can either be used by fully qualifying the static method like
+ * {@code GroovyAssert} methods can either be used by fully qualifying the static method like:
*
* <pre>
* groovy.test.GroovyAssert.shouldFail { ... }
* </pre>
*
- * or by importing the static methods with one ore more static imports
+ * or by importing the static methods with one ore more static imports:
*
* <pre>
* import static groovy.test.GroovyAssert.shouldFail
- * import static groovy.test.GroovyAssert.assertNotNull
* </pre>
* </p>
+ * <em>Backwards compatibility note:</em>
+ * Prior to Groovy 4, {@code GroovyAssert} extended JUnit 4's {@link org.junit.Assert} class.
+ * This meant that you could statically import static methods from that class via {@code GroovyAssert}, e.g.:
+ * <pre>
+ * import static groovy.test.GroovyAssert.assertNotNull
+ * </pre>
+ * This is generally regarded as a code smell since inheritance is primarily to do with instance methods.
+ * From Groovy 4, you should import such methods directly, e.g.:
+ * <pre>
+ * import static org.junit.Assert.assertNotNull
+ * </pre>
*
* @see groovy.test.GroovyTestCase
* @since 2.3
*/
-public class GroovyAssert extends org.junit.Assert {
+public class GroovyAssert {
private static final Logger log = Logger.getLogger(GroovyAssert.class.getName());
@@ -113,6 +124,19 @@ public class GroovyAssert extends org.junit.Assert {
return th;
}
+ private static void assertTrue(String message, boolean condition) {
+ if (!condition) {
+ fail(message);
+ }
+ }
+
+ public static void fail(String message) {
+ if (message == null) {
+ throw new AssertionError();
+ }
+ throw new AssertionError(message);
+ }
+
/**
* Asserts that the given code closure fails when it is evaluated
* and that a particular type of exception is thrown.
@@ -285,7 +309,7 @@ public class GroovyAssert extends org.junit.Assert {
return m;
}
}
- catch (final Exception e) {
+ catch (final Exception ignore) {
// can't access, ignore it
}
}
@@ -305,11 +329,20 @@ public class GroovyAssert extends org.junit.Assert {
final Class returnType = method.getReturnType();
return parameters.length == 0
- && (name.startsWith("test") || method.getAnnotation(Test.class) != null)
+ && (name.startsWith("test") || hasTestAnnotation(method))
&& returnType.equals(Void.TYPE)
&& Modifier.isPublic(method.getModifiers());
}
+ private static boolean hasTestAnnotation(Method method) {
+ for (Annotation annotation : method.getAnnotations()) {
+ if ("org.junit.Test".equals(annotation.annotationType().getName())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
/**
* <p>
* Runs the calling JUnit test again and fails only if it unexpectedly runs.<br>