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

Fix tests with junit 4.13 #182

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
13 changes: 11 additions & 2 deletions src/main/java/junitparams/internal/InvokeParameterisedMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;

/**
* JUnit invoker for parameterised test methods
Expand Down Expand Up @@ -209,8 +211,15 @@ private Object castParameterDirectly(Object object, Class clazz) {
return object.toString().charAt(0);
if (clazz.isAssignableFrom(Byte.TYPE) || clazz.isAssignableFrom(Byte.class))
return Byte.parseByte((String) object);
if (clazz.isAssignableFrom(BigDecimal.class))
return new BigDecimal((String) object);
if (clazz.isAssignableFrom(BigDecimal.class)) {
DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setParseBigDecimal(true);
try {
return decimalFormat.parse((String) object);
} catch (ParseException e) {
throw new IllegalArgumentException("Illegal BigDecimal value (" + object + ")", e);
}
}
PropertyEditor editor = PropertyEditorManager.findEditor(clazz);
if (editor != null) {
editor.setAsText((String) object);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/junitparams/BeforeAfterClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void shouldProvideHelpfulExceptionMessageWhenLifecycleAnnotationUsedImpro

assertThat(result.getFailureCount()).isEqualTo(1);
assertThat(result.getFailures().get(0).getException())
.hasMessage("Method fail() should be static");
.hasMessageContaining("Method fail() should be static");
}


Expand Down
49 changes: 17 additions & 32 deletions src/test/java/junitparams/FilterableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;

import static org.assertj.core.api.Assertions.*;
Expand All @@ -22,7 +23,7 @@ public void shouldRunAllTests() throws Exception {

@Test
public void shouldRunSingleTestWithoutParameters() throws Exception {
Request request = requestSingleMethodRun(SampleTestCase.class, "firstTestMethod");
Request request = Request.method(SampleTestCase.class, "firstTestMethod");

Result result = new JUnitCore().run(request);

Expand All @@ -31,7 +32,7 @@ public void shouldRunSingleTestWithoutParameters() throws Exception {

@Test
public void shouldRunParametrisedTest() throws Exception {
Request request = requestSingleMethodRun(SampleTestCase.class, "secondTestMethod");
Request request = Request.method(SampleTestCase.class, "secondTestMethod");

Result result = new JUnitCore().run(request);

Expand All @@ -40,43 +41,27 @@ public void shouldRunParametrisedTest() throws Exception {

@Test
public void shouldReturnOneDescriptionForSimpleTestCase() throws Exception {
Request request = requestSingleMethodRun(SampleTestCase.class, "firstTestMethod");
Request request = Request.method(SampleTestCase.class, "firstTestMethod");

Description description = request.getRunner().getDescription();
Runner runner = request.getRunner();
Description description = runner.getDescription();

assertThat(description.getChildren()).hasSize(1);
assertThat(description.getChildren().get(0).getChildren()).hasSize(0);
assertThat(runner.testCount() == 1);
for (Description desc: description.getChildren())
if (desc.getDisplayName() == "firstTestMethod")
assertThat(desc.getChildren()).hasSize(0);
}

@Test
public void shouldReturnParametrizedDescriptionsForParametrizedTestCase() throws Exception {
Request request = requestSingleMethodRun(SampleTestCase.class, "secondTestMethod");
Request request = Request.method(SampleTestCase.class, "secondTestMethod");

Description description = request.getRunner().getDescription();
Runner runner = request.getRunner();
Description description = runner.getDescription();

assertThat(description.getChildren()).hasSize(1);
assertThat(description.getChildren().get(0).getChildren()).hasSize(2);
}

private Request requestSingleMethodRun(Class<SampleTestCase> clazz, String methodName) {
return Request.aClass(clazz).filterWith(new SingleMethodFilter(methodName));
}

private static class SingleMethodFilter extends Filter {
private final String methodName;

public SingleMethodFilter(String methodName) {
this.methodName = methodName;
}

@Override
public boolean shouldRun(Description description) {
return description.getDisplayName().contains(methodName);
}

@Override
public String describe() {
return methodName;
}
assertThat(runner.testCount() == 1);
for (Description desc: description.getChildren())
if (desc.getDisplayName() == "secondTestMethod")
assertThat(desc.getChildren()).hasSize(2);
}
}
2 changes: 1 addition & 1 deletion src/test/java/junitparams/RulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void shouldProvideHelpfulExceptionMessageWhenRuleIsUsedImproperly() {

assertThat(result.getFailureCount()).isEqualTo(1);
assertThat(result.getFailures().get(0).getException())
.hasMessage("The @Rule 'testRule' must be public.");
.hasMessageContaining("The @Rule 'testRule' must be public.");
}

public class ProtectedRuleTest {
Expand Down