-
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.
Ensure classes annotated with @OrderWith do not have @FixMethodOrder.
This is needed because classes annotated with @FixMethodOrder will not be reordered or sorted, so having both annotations is a contradiction. - Add AnnotationValidator to fail if class annotated with @FixMethodOrder - Annotate OrderWith with @ValidateWith(OrderWithValidator.class) - Add tests for the new validator
- Loading branch information
1 parent
7c2f12c
commit b51fa17
Showing
4 changed files
with
91 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.junit.runner; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static java.util.Collections.singletonList; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.FixMethodOrder; | ||
import org.junit.runners.model.TestClass; | ||
import org.junit.validator.AnnotationValidator; | ||
|
||
/** | ||
* Validates that there are no errors in the use of the {@code OrderWith} | ||
* annotation. If there is, a {@code Throwable} object will be added to the list | ||
* of errors. | ||
* | ||
* @since 4.13 | ||
*/ | ||
public final class OrderWithValidator extends AnnotationValidator { | ||
|
||
/** | ||
* Adds to {@code errors} a throwable for each problem detected. Looks for | ||
* {@code FixMethodOrder} annotations. | ||
* | ||
* @param testClass that is being validated | ||
* @return A list of exceptions detected | ||
* | ||
* @since 4.13 | ||
*/ | ||
@Override | ||
public List<Exception> validateAnnotatedClass(TestClass testClass) { | ||
if (testClass.getAnnotation(FixMethodOrder.class) != null) { | ||
return singletonList( | ||
new Exception("@FixMethodOrder cannot be combined with @OrderWith")); | ||
} | ||
return emptyList(); | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
src/test/java/org/junit/runner/OrderWithValidatorTest.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,50 @@ | ||
package org.junit.runner; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.FixMethodOrder; | ||
import org.junit.Test; | ||
import org.junit.runner.manipulation.Alphanumeric; | ||
import org.junit.runners.JUnit4; | ||
import org.junit.runners.MethodSorters; | ||
import org.junit.runners.model.TestClass; | ||
|
||
public class OrderWithValidatorTest { | ||
private final OrderWithValidator validator = new OrderWithValidator(); | ||
|
||
@RunWith(JUnit4.class) | ||
@OrderWith(Alphanumeric.class) | ||
public static class TestWithNoValidationErrors { | ||
@Test | ||
public void passes() {} | ||
} | ||
|
||
@RunWith(JUnit4.class) | ||
@OrderWith(Alphanumeric.class) | ||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
public static class TestAnnotatedWithFixMethodOrder { | ||
@Test | ||
public void passes() {} | ||
} | ||
|
||
@Test | ||
public void noErrorIsAddedForTestWithoutValdationErrors() { | ||
List<Exception> errors = validator.validateAnnotatedClass( | ||
new TestClass(TestWithNoValidationErrors.class)); | ||
|
||
assertThat(errors.size(), is(0)); | ||
} | ||
|
||
@Test | ||
public void errorIsAddedWhenTestAnnotatedWithFixMethodOrder() { | ||
List<Exception> errors = validator.validateAnnotatedClass( | ||
new TestClass(TestAnnotatedWithFixMethodOrder.class)); | ||
|
||
assertThat(errors.size(), is(1)); | ||
Exception exception = errors.get(0); | ||
assertThat(exception.getMessage(), is("@FixMethodOrder cannot be combined with @OrderWith")); | ||
} | ||
} |