Skip to content

Commit

Permalink
handled initialization errors caused by junit methods signature
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejmikosik committed Aug 15, 2018
1 parent b9927c3 commit 8651bff
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
21 changes: 21 additions & 0 deletions main/java/org/quackery/junit/QuackeryRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ private static List<Test> instantiateFailingTestsExplainingCausesOf(Initializati
continue;
} else if (noPublicDefaultConstructor(cause) && !hasJunitTestMethods) {
continue;
} else if (incorrectJunitTestMethod(cause)) {
testsExplainingErrors.add(new Case(junitTestMethodName(cause)) {
public void run() throws Throwable {
throw cause;
}
});
} else {
testsExplainingErrors.add(new Case(cause.getMessage()) {
public void run() throws Throwable {
Expand Down Expand Up @@ -150,6 +156,21 @@ private static boolean noPublicDefaultConstructor(Throwable cause) {
|| message.equals("Test class should have exactly one public zero-argument constructor");
}

private static boolean incorrectJunitTestMethod(Throwable cause) {
String message = cause.getMessage();
return message.startsWith("Method")
&& message.contains(" should ");
}

private static String junitTestMethodName(Throwable cause) {
String message = cause.getMessage();
int begin = "Method ".length();
int end = message.contains("() should")
? message.indexOf("() should")
: message.indexOf(" should");
return message.substring(begin, end);
}

private Description describe(Test test) {
if (test instanceof Suite) {
return describe((Suite) test);
Expand Down
65 changes: 65 additions & 0 deletions test/java/org/quackery/junit/TestQuackeryRunnerInitialization.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.quackery.junit;

import static java.lang.String.format;
import static java.lang.reflect.Modifier.PRIVATE;
import static java.lang.reflect.Modifier.PUBLIC;
import static java.lang.reflect.Modifier.STATIC;
Expand Down Expand Up @@ -96,6 +97,70 @@ public void reports_quackery_annotated_method_having_parameters() {
assertEquals(failure.getException().getMessage(), "method cannot have parameters");
}

public void reports_junit_test_annotated_method_being_not_public() {
result = run(new JunitClassBuilder()
.define(defaultJunitMethod()
.name(methodName)
.modifiers(PRIVATE))
.load());

assertEquals(result.getFailureCount(), 1);
Failure failure = result.getFailures().get(0);
assertEquals(failure.getDescription().getMethodName(), methodName);
assertEquals(failure.getException().getClass(), Exception.class);
assertEquals(
failure.getException().getMessage(),
format("Method %s() should be public", methodName));
}

public void reports_junit_test_annotated_method_being_static() {
result = run(new JunitClassBuilder()
.define(defaultJunitMethod()
.name(methodName)
.modifiers(PUBLIC | STATIC))
.load());

assertEquals(result.getFailureCount(), 1);
Failure failure = result.getFailures().get(0);
assertEquals(failure.getDescription().getMethodName(), methodName);
assertEquals(failure.getException().getClass(), Exception.class);
assertEquals(
failure.getException().getMessage(),
format("Method %s() should not be static", methodName));
}

public void reports_junit_test_annotated_method_wrong_return_type() {
result = run(new JunitClassBuilder()
.define(defaultJunitMethod()
.name(methodName)
.returnType(Object.class))
.load());

assertEquals(result.getFailureCount(), 1);
Failure failure = result.getFailures().get(0);
assertEquals(failure.getDescription().getMethodName(), methodName);
assertEquals(failure.getException().getClass(), Exception.class);
assertEquals(
failure.getException().getMessage(),
format("Method %s() should be void", methodName));
}

public void reports_junit_test_annotated_method_having_parameters() {
result = run(new JunitClassBuilder()
.define(defaultJunitMethod()
.name(methodName)
.parameters(Object.class))
.load());

assertEquals(result.getFailureCount(), 1);
Failure failure = result.getFailures().get(0);
assertEquals(failure.getDescription().getMethodName(), methodName);
assertEquals(failure.getException().getClass(), Exception.class);
assertEquals(
failure.getException().getMessage(),
format("Method %s should have no parameters", methodName));
}

public void reports_missing_default_constructor() {
result = run(new JunitClassBuilder(NO_CONSTRUCTORS)
.define(defaultJunitMethod())
Expand Down

0 comments on commit 8651bff

Please sign in to comment.