Browse Source

ZOOKEEPER-2174 JUnit4ZKTestRunner logs test failure for all exceptions

JUnit4ZKTestRunner logs test failure for all exceptions, even if the test
method is annotated with an expected exception (Chris Nauroth via rgs).

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1677460 13f79535-47bb-0310-9956-ffa450edef68
Raúl Gutiérrez Segalés 10 năm trước cách đây
mục cha
commit
b92709ede9
2 tập tin đã thay đổi với 21 bổ sung4 xóa
  1. 4 0
      CHANGES.txt
  2. 17 4
      src/java/test/org/apache/zookeeper/JUnit4ZKTestRunner.java

+ 4 - 0
CHANGES.txt

@@ -82,6 +82,10 @@ BUGFIXES:
   ZOOKEEPER-2173. ZK startup failure should be handled with proper error message
   (J.Andreina via camille)
 
+  ZOOKEEPER-2174 JUnit4ZKTestRunner logs test failure for all exceptions even
+  if the test method is annotated with an expected exception (Chris Nauroth
+  via rgs)
+
 IMPROVEMENTS:
   ZOOKEEPER-1660 Documentation for Dynamic Reconfiguration (Reed Wanderman-Milne via shralex)  
 

+ 17 - 4
src/java/test/org/apache/zookeeper/JUnit4ZKTestRunner.java

@@ -20,6 +20,7 @@ package org.apache.zookeeper;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.junit.Test;
 import org.junit.internal.runners.statements.InvokeMethod;
 import org.junit.runners.BlockJUnit4ClassRunner;
 import org.junit.runners.model.FrameworkMethod;
@@ -38,16 +39,18 @@ public class JUnit4ZKTestRunner extends BlockJUnit4ClassRunner {
     }
 
     public static class LoggedInvokeMethod extends InvokeMethod {
-        private String name;
+        private final FrameworkMethod method;
+        private final String name;
 
         public LoggedInvokeMethod(FrameworkMethod method, Object target) {
             super(method, target);
+            this.method = method;
             name = method.getName();
         }
 
         @Override
         public void evaluate() throws Throwable {
-            LOG.info("RUNNING TEST METHOD " + name);
+            LOG.info("RUNNING TEST METHOD {}", name);
             try {
                 super.evaluate();
                 Runtime rt = Runtime.getRuntime();
@@ -59,10 +62,20 @@ public class JUnit4ZKTestRunner extends BlockJUnit4ClassRunner {
                 }
                 LOG.info("Number of threads {}", tg.activeCount());
             } catch (Throwable t) {
-                LOG.info("TEST METHOD FAILED " + name, t);
+                // The test method threw an exception, but it might be an
+                // expected exception as defined in the @Test annotation.
+                // Check the annotation and log an appropriate message.
+                Test annotation = this.method.getAnnotation(Test.class);
+                if (annotation != null && annotation.expected() != null &&
+                        annotation.expected().isAssignableFrom(t.getClass())) {
+                    LOG.info("TEST METHOD {} THREW EXPECTED EXCEPTION {}", name,
+                        annotation.expected());
+                } else {
+                    LOG.info("TEST METHOD FAILED {}", name, t);
+                }
                 throw t;
             }
-            LOG.info("FINISHED TEST METHOD " + name);
+            LOG.info("FINISHED TEST METHOD {}", name);
         }
     }