Skip to content
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

Adding support for additional annotations #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/main/java/junitparams/internal/ParametrizedDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
import junitparams.naming.TestCaseNamingStrategy;
import org.junit.runner.Description;

import java.lang.annotation.Annotation;


class ParametrizedDescription {

private TestCaseNamingStrategy namingStrategy;
private String testClassName;
private String methodName;
private Annotation[] annotations;

ParametrizedDescription(TestCaseNamingStrategy namingStrategy, String testClassName, String methodName) {
ParametrizedDescription(TestCaseNamingStrategy namingStrategy, String testClassName, String methodName,
final Annotation[] annotations) {
this.namingStrategy = namingStrategy;
this.testClassName = testClassName;
this.methodName = methodName;
this.annotations = annotations;
}

Description parametrizedDescription(Object[] params) {
Expand All @@ -22,9 +28,13 @@ Description parametrizedDescription(Object[] params) {
String name = namingStrategy.getTestCaseName(i, paramSet);
String uniqueMethodId = Utils.uniqueMethodId(i, paramSet, methodName);
parametrised.addChild(
Description.createTestDescription(testClassName, name, uniqueMethodId)
Description.createSuiteDescription(formatDisplayName(testClassName, name), uniqueMethodId, annotations)
);
}
return parametrised;
}

private String formatDisplayName(String className, String name) {
return String.format("%s(%s)", name, className);
}
}
2 changes: 1 addition & 1 deletion src/main/java/junitparams/internal/TestMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected Description computeValue() {

if (isNotIgnored() && !describeFlat()) {
TestCaseNamingStrategy namingStrategy = new MacroSubstitutionNamingStrategy(getAnnotation(TestCaseName.class), name());
return new ParametrizedDescription(namingStrategy, testClass.getJavaClass().getName(), name()).parametrizedDescription(parametersSets());
return new ParametrizedDescription(namingStrategy, testClass.getJavaClass().getName(), name(), frameworkMethod.getAnnotations()).parametrizedDescription(parametersSets());
} else {
return Description.createTestDescription(testClass.getJavaClass(), name(), frameworkMethodAnnotations.allAnnotations());
}
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/junitparams/AdditionalAnnotationsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package junitparams;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runners.model.Statement;

import java.lang.annotation.Annotation;
import java.util.Collection;

import static org.assertj.core.api.Assertions.assertThat;


@RunWith(JUnitParamsRunner.class)
public class AdditionalAnnotationsTest {

@Rule
public OtherRule rule = new OtherRule();

@Test
@Parameters({"test"})
public void additionalAnnotationTestWithParameters(String param) {
assertThat(param).isEqualTo("test");
assertThat(rule.annotations).isNotEmpty();
}

@Test
@Parameters(method = "stringParams")
public void additionalAnnotationTestWithMethod(String param) {
assertThat(param).startsWith("stringParam");
assertThat(rule.annotations).isNotEmpty();
}

private String[] stringParams() {
return new String[] {"stringParam1", "stringParam2", "stringParam3"};
}

private class OtherRule implements TestRule {
private Collection<Annotation> annotations;
@Override
public Statement apply(final Statement base, final Description description) {
this.annotations = description.getAnnotations();
return base;
}
}
}