Skip to content

Commit

Permalink
@IgnoreUntil annotation can now be on a method only.
Browse files Browse the repository at this point in the history
@IgnoreUntil method annotation overrides class annotation when non-default value set.
Fixes issue unruly#3
  • Loading branch information
simon.dudley committed Feb 18, 2018
1 parent 953f34d commit ee34f7d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/co/unruly/junit/IgnoreUntilRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

public class IgnoreUntilRule implements TestRule {

static final String DEFAULT_IGNORE = "2000-01-01";

@Override
public Statement apply(Statement base, Description description) {

IgnoreUntil ignoreUntil = description.getAnnotation(IgnoreUntil.class);

if (ignoreUntil != null && description.getTestClass() != null) {
if (ignoreUntil != null && ignoreUntil.value().equals(DEFAULT_IGNORE) && description.getTestClass() != null) {
ignoreUntil = description.getTestClass().getAnnotation(IgnoreUntil.class);
}

Expand Down
52 changes: 52 additions & 0 deletions src/test/java/co/unruly/junit/IgnoreUntilRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.lang.annotation.Annotation;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -50,4 +52,54 @@ public void shouldReturnAlwaysPassesStatementIfIgnoreUntilDateIsInTheFuture() {

assertTrue(rule.apply(mockStatement, mockDescription) instanceof IgnoreUntilRule.AlwaysPassesStatement);
}

@Test
public void shouldReturnOriginalStatementIfMethodAnnotationWithNonDefaultValueExists() throws NoSuchMethodException {
Annotation[] methodAnnotations = (MethodOverrideClassAnnotationTest.class).getMethod("shouldNowExecute").getDeclaredAnnotations();
Description testMethodDescription = Description.createTestDescription(MethodOverrideClassAnnotationTest.class, "shouldNowExecute", methodAnnotations);
assertEquals(2, testMethodDescription.getAnnotations().size());

assertEquals(mockStatement, rule.apply(mockStatement, testMethodDescription));
}

@IgnoreUntil("2099-01-01")
static class MethodOverrideClassAnnotationTest {

@IgnoreUntil("2018-01-01")
@Test
public void shouldNowExecute() { }
}

@Test
public void shouldReturnAlwaysPassesIfMethodAnnotationInheritsFromClassAnnotation() throws NoSuchMethodException {
Annotation[] methodAnnotations = (MethodInheritFromClassAnnotationTest.class).getMethod("shouldNowExecute").getDeclaredAnnotations();
Description testMethodDescription = Description.createTestDescription(MethodInheritFromClassAnnotationTest.class, "shouldNowExecute", methodAnnotations);
assertEquals(2, testMethodDescription.getAnnotations().size());

assertTrue(rule.apply(mockStatement, testMethodDescription) instanceof IgnoreUntilRule.AlwaysPassesStatement);
}

@IgnoreUntil("2099-01-01")
static class MethodInheritFromClassAnnotationTest {

@IgnoreUntil
@Test
public void shouldNowExecute() { }
}

@Test
public void shouldReturnAlwaysPassesIfOnlyMethodAnnotationExists() throws NoSuchMethodException {
Annotation[] methodAnnotations = (MethodAnnotationOnly.class).getMethod("shouldNowExecute").getDeclaredAnnotations();
Description testMethodDescription = Description.createTestDescription(MethodAnnotationOnly.class, "shouldNowExecute", methodAnnotations);
assertEquals(2, testMethodDescription.getAnnotations().size());

assertTrue(rule.apply(mockStatement, testMethodDescription) instanceof IgnoreUntilRule.AlwaysPassesStatement);
}

static class MethodAnnotationOnly {

@IgnoreUntil("2099-01-01")
@Test
public void shouldNowExecute() { }
}
}

0 comments on commit ee34f7d

Please sign in to comment.