-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide better feedback to the user in case of invalid test classes (#…
…1286) Only one exception per invalid test class is now fired, rather than one per validation error, with all of the validation errors as part of its message. Example: org.junit.runners.InvalidTestClassError: Invalid test class 'InvalidTest': 1. Method staticAfterMethod() should not be static 2. Method staticBeforeMethod() should not be static Validation errors for the same test class now count only once in the failure count (Result#getFailureCount). Implementation: - ParentRunner#validate throws InvalidTestClassError in case of validation errors - ErrorReportingRunner fires the InvalidTestClassError above without unpacking the causes
- Loading branch information
1 parent
8fb8153
commit 0e4cb7e
Showing
12 changed files
with
149 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/org/junit/runners/model/InvalidTestClassError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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 String message; | ||
|
||
public InvalidTestClassError(Class<?> offendingTestClass, List<Throwable> validationErrors) { | ||
super(validationErrors); | ||
this.message = createMessage(offendingTestClass, 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/test/java/org/junit/runners/model/InvalidTestClassErrorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.junit.runners.model; | ||
|
||
import org.junit.Test; | ||
|
||
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 { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters