aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/internal/runners/MethodValidator.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/junit/internal/runners/MethodValidator.java')
-rw-r--r--src/main/java/org/junit/internal/runners/MethodValidator.java120
1 files changed, 63 insertions, 57 deletions
diff --git a/src/main/java/org/junit/internal/runners/MethodValidator.java b/src/main/java/org/junit/internal/runners/MethodValidator.java
index cadc93f..ba9c9d1 100644
--- a/src/main/java/org/junit/internal/runners/MethodValidator.java
+++ b/src/main/java/org/junit/internal/runners/MethodValidator.java
@@ -15,77 +15,83 @@ import org.junit.runners.BlockJUnit4ClassRunner;
/**
* @deprecated Included for backwards compatibility with JUnit 4.4. Will be
- * removed in the next release. Please use
+ * removed in the next major release. Please use
* {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}.
*/
@Deprecated
public class MethodValidator {
- private final List<Throwable> fErrors= new ArrayList<Throwable>();
+ private final List<Throwable> errors = new ArrayList<Throwable>();
- private TestClass fTestClass;
+ private TestClass testClass;
- public MethodValidator(TestClass testClass) {
- fTestClass = testClass;
- }
+ public MethodValidator(TestClass testClass) {
+ this.testClass = testClass;
+ }
- public void validateInstanceMethods() {
- validateTestMethods(After.class, false);
- validateTestMethods(Before.class, false);
- validateTestMethods(Test.class, false);
-
- List<Method> methods= fTestClass.getAnnotatedMethods(Test.class);
- if (methods.size() == 0)
- fErrors.add(new Exception("No runnable methods"));
- }
+ public void validateInstanceMethods() {
+ validateTestMethods(After.class, false);
+ validateTestMethods(Before.class, false);
+ validateTestMethods(Test.class, false);
- public void validateStaticMethods() {
- validateTestMethods(BeforeClass.class, true);
- validateTestMethods(AfterClass.class, true);
- }
-
- public List<Throwable> validateMethodsForDefaultRunner() {
- validateNoArgConstructor();
- validateStaticMethods();
- validateInstanceMethods();
- return fErrors;
- }
-
- public void assertValid() throws InitializationError {
- if (!fErrors.isEmpty())
- throw new InitializationError(fErrors);
- }
+ List<Method> methods = testClass.getAnnotatedMethods(Test.class);
+ if (methods.size() == 0) {
+ errors.add(new Exception("No runnable methods"));
+ }
+ }
- public void validateNoArgConstructor() {
- try {
- fTestClass.getConstructor();
- } catch (Exception e) {
- fErrors.add(new Exception("Test class should have public zero-argument constructor", e));
- }
- }
+ public void validateStaticMethods() {
+ validateTestMethods(BeforeClass.class, true);
+ validateTestMethods(AfterClass.class, true);
+ }
- private void validateTestMethods(Class<? extends Annotation> annotation,
- boolean isStatic) {
- List<Method> methods= fTestClass.getAnnotatedMethods(annotation);
-
- for (Method each : methods) {
- if (Modifier.isStatic(each.getModifiers()) != isStatic) {
- String state= isStatic ? "should" : "should not";
- fErrors.add(new Exception("Method " + each.getName() + "() "
+ public List<Throwable> validateMethodsForDefaultRunner() {
+ validateNoArgConstructor();
+ validateStaticMethods();
+ validateInstanceMethods();
+ return errors;
+ }
+
+ public void assertValid() throws InitializationError {
+ if (!errors.isEmpty()) {
+ throw new InitializationError(errors);
+ }
+ }
+
+ public void validateNoArgConstructor() {
+ try {
+ testClass.getConstructor();
+ } catch (Exception e) {
+ errors.add(new Exception("Test class should have public zero-argument constructor", e));
+ }
+ }
+
+ private void validateTestMethods(Class<? extends Annotation> annotation,
+ boolean isStatic) {
+ List<Method> methods = testClass.getAnnotatedMethods(annotation);
+
+ for (Method each : methods) {
+ if (Modifier.isStatic(each.getModifiers()) != isStatic) {
+ String state = isStatic ? "should" : "should not";
+ errors.add(new Exception("Method " + each.getName() + "() "
+ state + " be static"));
- }
- if (!Modifier.isPublic(each.getDeclaringClass().getModifiers()))
- fErrors.add(new Exception("Class " + each.getDeclaringClass().getName()
+ }
+ if (!Modifier.isPublic(each.getDeclaringClass().getModifiers())) {
+ errors.add(new Exception("Class " + each.getDeclaringClass().getName()
+ " should be public"));
- if (!Modifier.isPublic(each.getModifiers()))
- fErrors.add(new Exception("Method " + each.getName()
+ }
+ if (!Modifier.isPublic(each.getModifiers())) {
+ errors.add(new Exception("Method " + each.getName()
+ " should be public"));
- if (each.getReturnType() != Void.TYPE)
- fErrors.add(new Exception("Method " + each.getName()
+ }
+ if (each.getReturnType() != Void.TYPE) {
+ errors.add(new Exception("Method " + each.getName()
+ " should be void"));
- if (each.getParameterTypes().length != 0)
- fErrors.add(new Exception("Method " + each.getName()
+ }
+ if (each.getParameterTypes().length != 0) {
+ errors.add(new Exception("Method " + each.getName()
+ " should have no parameters"));
- }
- }
+ }
+ }
+ }
}