-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide better feedback to the user in case of invalid test classes #1286
Changes from 17 commits
9f0e1a0
5c91a16
93a315a
4a6e166
fa8381b
2c3a896
45e06ba
ef98f6f
7e7787b
4765762
d5fc61c
2147b76
e96a5a5
feec1be
800ad80
eedafa8
5e80291
a1610b6
3c73d3e
a227269
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.junit.runners.model; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Thrown by {@link org.junit.runner.Runner}s in case the class under test is not valid. | ||
* <p> | ||
* Its message conveniently lists all of the validation errors. | ||
* | ||
* @since 4.13 | ||
*/ | ||
public class InvalidTestClassError extends InitializationError { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final Class<?> testClass; | ||
private final String message; | ||
|
||
public InvalidTestClassError(Class<?> offendingTestClass, List<Throwable> validationErrors) { | ||
super(validationErrors); | ||
this.testClass = offendingTestClass; | ||
this.message = createMessage(testClass, validationErrors); | ||
} | ||
|
||
private static String createMessage(Class<?> testClass, List<Throwable> validationErrors) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(String.format("Invalid test class '%s':", testClass.getName())); | ||
int i = 1; | ||
for (Throwable error : validationErrors) { | ||
sb.append("\n " + (i++) + ". " + error.getMessage()); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* @return a message with a list of all of the validation errors | ||
*/ | ||
@Override | ||
public String getMessage() { | ||
return message; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,24 @@ | ||
package org.junit.internal.runners; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.JUnitCore; | ||
import org.junit.runner.Result; | ||
import org.junit.runner.notification.Failure; | ||
import org.junit.runner.notification.RunNotifier; | ||
import org.junit.runners.model.InvalidTestClassError; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.CoreMatchers.allOf; | ||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.startsWith; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class ErrorReportingRunnerTest { | ||
|
||
|
@@ -23,5 +41,48 @@ public void cannotCreateWithNullClasses() { | |
public void cannotCreateWithoutClass() { | ||
new ErrorReportingRunner(new RuntimeException()); | ||
} | ||
|
||
|
||
@Test | ||
public void givenInvalidTestClassErrorAsCause() { | ||
final List<Failure> firedFailures = new ArrayList<Failure>(); | ||
InvalidTestClassError testClassError = new InvalidTestClassError(TestClassWithErrors.class, | ||
Arrays.asList(new Throwable("validation error 1"), new Throwable("validation error 2"))); | ||
ErrorReportingRunner sut = new ErrorReportingRunner(TestClassWithErrors.class, testClassError); | ||
|
||
sut.run(new RunNotifier() { | ||
@Override | ||
public void fireTestFailure(Failure failure) { | ||
super.fireTestFailure(failure); | ||
firedFailures.add(failure); | ||
} | ||
}); | ||
|
||
assertThat(firedFailures.size(), is(1)); | ||
Throwable exception = firedFailures.get(0).getException(); | ||
assertThat(exception, instanceOf(InvalidTestClassError.class)); | ||
assertThat(((InvalidTestClassError) exception), is(testClassError)); | ||
} | ||
|
||
@Test | ||
public void givenInvalidTestClass_integrationTest() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please note the integration test here. I'm not sure if this is a good place for it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine. Thanks! |
||
Result result = JUnitCore.runClasses(TestClassWithErrors.class); | ||
|
||
assertThat(result.getFailureCount(), is(1)); | ||
Throwable failure = result.getFailures().get(0).getException(); | ||
assertThat(failure, instanceOf(InvalidTestClassError.class)); | ||
assertThat(failure.getMessage(), allOf( | ||
startsWith("Invalid test class '" + TestClassWithErrors.class.getName() + "'"), | ||
containsString("\n 1. "), | ||
containsString("\n 2. ") | ||
)); | ||
} | ||
|
||
private static class TestClassWithErrors { | ||
@Before public static void staticBeforeMethod() {} | ||
@After public static void staticAfterMethod() {} | ||
|
||
@Test public String testMethodReturningString() { | ||
return "this should not be allowed"; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.junit.runners.model; | ||
|
||
import org.junit.Test; | ||
import org.junit.runners.model.InvalidTestClassError; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This import is unused now, isn't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, thanks! Fixed now |
||
|
||
import static java.util.Arrays.asList; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class InvalidTestClassErrorTest { | ||
|
||
@Test | ||
public void invalidTestClassErrorShouldListAllValidationErrorsInItsMessage() { | ||
InvalidTestClassError sut = new InvalidTestClassError(SampleTestClass.class, | ||
asList(new Throwable("validation error 1"), new Throwable("validation error 2"))); | ||
|
||
assertThat(sut.getMessage(), equalTo("Invalid test class '" + SampleTestClass.class.getName() + "':" + | ||
"\n 1. validation error 1" + | ||
"\n 2. validation error 2")); | ||
} | ||
|
||
private static class SampleTestClass { | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we have to store
testClass
since it's only used in the constructor.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uhm ya right.