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

Fixes #151: org.junit.runner.Description.fAnnotations array is null with junitparams parameterized junit tests #158

Open
wants to merge 3 commits 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
8 changes: 1 addition & 7 deletions src/main/java/junitparams/JUnitParamsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ protected void runChild(FrameworkMethod method, RunNotifier notifier) {
super.runChild(method, notifier);
}
}

private boolean initializedProperly(Statement statement, RunNotifier notifier) {
boolean initializedProperly = true;
if (statement instanceof Fail) {
Expand All @@ -470,12 +470,6 @@ private boolean initializedProperly(Statement statement, RunNotifier notifier) {
return initializedProperly;
}

@Override
protected Description describeChild(FrameworkMethod method) {
Description description = parameterisedRunner.getDescriptionFor(method);
return description == null ? super.describeChild(method) : description;
}

private void verifyMethodCanBeRunByStandardRunner(TestMethod testMethod) {
List<Throwable> errors = new ArrayList<Throwable>();
testMethod.frameworkMethod().validatePublicVoidNoArg(false, errors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public Description describeParameterisedMethod(FrameworkMethod method) {
if (!testMethod.isParameterised())
return null;

return testMethod.description();
return testMethod.describe();
}

/**
Expand All @@ -174,24 +174,4 @@ public TestMethod testMethodFor(FrameworkMethod method) {
return testMethods.get(method);
}

/**
* Returns description of parametrized method for given set of parameters (considering test name convention
* given with @{@link junitparams.naming.TestCaseName}). If method is not parametrized or runner has not been created yet
* returns null.
*
* @param method
* @return description of given method
*/
public Description getDescriptionFor(FrameworkMethod method) {
TestMethod testMethod = testMethodFor(method);
if (shouldRun(testMethod)) {
ParameterisedTestMethodRunner runner = parameterisedMethods.get(testMethod);
return wasInvoked(runner) ? runner.currentTestDescription() : null;
}
return null;
}

private boolean wasInvoked(ParameterisedTestMethodRunner runner) {
return runner != null && runner.count() != 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Object currentParamsFromAnnotation() {
}

void runTestMethod(Statement methodInvoker, RunNotifier notifier) {
Description methodWithParams = findChildForParams(methodInvoker, method.description());
Description methodWithParams = findChildForParams(methodInvoker, method.describe());

runMethodInvoker(notifier, methodInvoker, methodWithParams);
}
Expand Down Expand Up @@ -105,8 +105,4 @@ private Statement getOriginalStatement(Statement methodInvoker, Field field) {
return null;
}
}

Description currentTestDescription() {
return method.description().getChildren().get(count - 1);
}
}
9 changes: 5 additions & 4 deletions src/main/java/junitparams/internal/TestMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
return frameworkMethodAnnotations.getAnnotation(annotationType);
}

Description describe() {
return description.get();
}


public Object[] parametersSets() {
return parameters.get();
}
Expand All @@ -128,8 +133,4 @@ public FrameworkMethod frameworkMethod() {
boolean isParameterised() {
return frameworkMethodAnnotations.isParametrised();
}

Description description() {
return description.get();
}
}
3 changes: 1 addition & 2 deletions src/test/java/junitparams/RulesTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package junitparams;

import junitparams.naming.TestCaseName;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
Expand Down Expand Up @@ -32,9 +31,9 @@ public class RulesTest {
@Rule
public Timeout timeout = new Timeout(0);


@Test
@Parameters("")
@TestCaseName("{method}")
public void shouldHandleRulesProperly(String n) {
assertThat(testName.getMethodName()).isEqualTo("shouldHandleRulesProperly");
}
Expand Down

This file was deleted.

6 changes: 3 additions & 3 deletions src/test/java/junitparams/internal/TestMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void forOthersToWorkWithArray(String... arg) throws Exception {
public void flatTestMethodStructure() throws Exception {
System.setProperty("JUnitParams.flat", "true");

Description description = plainTestMethod.description();
Description description = plainTestMethod.describe();

assertEquals("for_others_to_work(junitparams.internal.TestMethodTest)", description.getDisplayName());
assertTrue(description.getChildren().isEmpty());
Expand All @@ -57,7 +57,7 @@ public void flatTestMethodStructure() throws Exception {
@Test
public void hierarchicalTestMethodStructure() throws Exception {
System.clearProperty("JUnitParams.flat");
Description description = plainTestMethod.description();
Description description = plainTestMethod.describe();

assertEquals("forOthersToWork", description.getDisplayName());
assertEquals("forOthersToWork(a) [0](junitparams.internal.TestMethodTest)", description.getChildren().get(0).getDisplayName());
Expand All @@ -67,7 +67,7 @@ public void hierarchicalTestMethodStructure() throws Exception {
@Test
public void hierarchicalArrayTestMethodStructure() throws Exception {
System.clearProperty("JUnitParams.flat");
Description description = arrayTestMethod.description();
Description description = arrayTestMethod.describe();

assertEquals("forOthersToWorkWithArray", description.getDisplayName());
assertEquals("forOthersToWorkWithArray(a,b) [0](junitparams.internal.TestMethodTest)",
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/junitparams/rules/MethodAnnotationInfoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package junitparams.rules;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
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 MethodAnnotationInfoTest {

@Rule
public final TestDescription testDescription = new TestDescription();

@Test
@Parameters({""})
public void annotationInfoInDescriptionPresent(String n) {
assertThat(testDescription.getAnnotations()).isNotEmpty();
}

public static class TestDescription implements TestRule {

private Description description;

@Override
public Statement apply(Statement statement, Description description) {
this.description = description;
return statement;
}

Collection<Annotation> getAnnotations() {
return description.getAnnotations();
}
}
}

This file was deleted.