Skip to content

Parameterized reuses TestClass #1449

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

Merged
merged 4 commits into from
May 26, 2017
Merged
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
7 changes: 6 additions & 1 deletion src/main/java/org/junit/experimental/theories/Theories.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public Theories(Class<?> klass) throws InitializationError {
super(klass);
}

/** @since 4.13 */
protected Theories(TestClass testClass) throws InitializationError {
super(testClass);
}

@Override
protected void collectInitializationErrors(List<Throwable> errors) {
super.collectInitializationErrors(errors);
Expand Down Expand Up @@ -215,7 +220,7 @@ protected void runWithIncompleteAssignment(Assignments incomplete)

protected void runWithCompleteAssignment(final Assignments complete)
throws Throwable {
new BlockJUnit4ClassRunner(getTestClass().getJavaClass()) {
new BlockJUnit4ClassRunner(getTestClass()) {
@Override
protected void collectInitializationErrors(
List<Throwable> errors) {
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/org/junit/internal/Checks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.junit.internal;

/** @since 4.13 */
public final class Checks {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not extract a Checks class and simply inline the validations, in order to not provide methods someone may use and depend on. I know that is a very weak argument. I leave it up to you whether you want to keep it or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see in the commits, we have multiple classes that do null checks. Quite often when I write code in JUnit I find myself wanting a one-liner to check for null.

I doubt anyone would depend on this class, since 1) it's in an internal package, 2) it has an odd name, and 3) most everyone that is using JDK 6 or later would use Objects.requireNotNull() or Guava's Preconditions.checkNotNull().


private Checks() {}

/**
* Checks that the given value is not {@code null}.
*
* @param value object reference to check
* @return the passed-in value, if not {@code null}
* @throws NullPointerException if {@code value} is {@code null}
*/
public static <T> T notNull(T value) {
if (value == null) {
throw new NullPointerException();
}
return value;
}

/**
* Checks that the given value is not {@code null}, using the given message
* as the exception message if an exception is thrown.
*
* @param value object reference to check
* @param message message to use if {@code value} is {@code null}
* @return the passed-in value, if not {@code null}
* @throws NullPointerException if {@code value} is {@code null}
*/
public static <T> T notNull(T value, String message) {
if (value == null) {
throw new NullPointerException(message);
}
return value;
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/junit/internal/builders/JUnit4Builder.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.junit.internal.builders;

import org.junit.runner.Runner;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.JUnit4;
import org.junit.runners.model.RunnerBuilder;

public class JUnit4Builder extends RunnerBuilder {
@Override
public Runner runnerForClass(Class<?> testClass) throws Throwable {
return new BlockJUnit4ClassRunner(testClass);
return new JUnit4(testClass);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is unrelated to the reuse of TestClass. It should be a separate commit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a separate commit. See 11822c4

I have this commit in this pull since there's no reason for ParentRunner.createTestClass() to be called for JUnit4-style tests that aren't annotated with RunWith.

Copy link
Contributor

@stefanbirkner stefanbirkner May 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my fault. I was not aware that the PR is made up of multiple commits.

11 changes: 11 additions & 0 deletions src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestClass;
import org.junit.validator.PublicClassValidator;
import org.junit.validator.TestClassValidator;

Expand Down Expand Up @@ -71,6 +72,16 @@ public BlockJUnit4ClassRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}

/**
* Creates a BlockJUnit4ClassRunner to run {@code testClass}.
*
* @throws InitializationError if the test class is malformed.
* @since 4.13
*/
protected BlockJUnit4ClassRunner(TestClass testClass) throws InitializationError {
super(testClass);
}

//
// Implementation of ParentRunner
//
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/junit/runners/JUnit4.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.junit.runners;

import org.junit.runners.model.InitializationError;
import org.junit.runners.model.TestClass;

/**
* Aliases the current default JUnit 4 class runner, for future-proofing. If
Expand All @@ -19,6 +20,6 @@ public final class JUnit4 extends BlockJUnit4ClassRunner {
* Constructs a new instance of the default runner
*/
public JUnit4(Class<?> klass) throws InitializationError {
super(klass);
super(new TestClass(klass));
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/junit/runners/ParentRunner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.junit.runners;

import static org.junit.internal.Checks.notNull;
import static org.junit.internal.runners.rules.RuleMemberValidator.CLASS_RULE_METHOD_VALIDATOR;
import static org.junit.internal.runners.rules.RuleMemberValidator.CLASS_RULE_VALIDATOR;

Expand Down Expand Up @@ -86,6 +87,21 @@ protected ParentRunner(Class<?> testClass) throws InitializationError {
validate();
}

/**
* Constructs a new {@code ParentRunner} that will run the {@code TestClass}.
*
* @since 4.13
*/
protected ParentRunner(TestClass testClass) throws InitializationError {
this.testClass = notNull(testClass);
validate();
}

/**
* @deprecated Please use {@link #ParentRunner(org.junit.runners.model.TestClass)}.
* @since 4.12
*/
@Deprecated
protected TestClass createTestClass(Class<?> testClass) {
return new TestClass(testClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private enum InjectionType {

public BlockJUnit4ClassRunnerWithParameters(TestWithParameters test)
throws InitializationError {
super(test.getTestClass().getJavaClass());
super(test.getTestClass());
parameters = test.getParameters().toArray(
new Object[test.getParameters().size()]);
name = test.getName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.junit.runners.parameterized;

import static java.util.Collections.unmodifiableList;
import static org.junit.internal.Checks.notNull;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -73,10 +74,4 @@ public String toString() {
return testClass.getName() + " '" + name + "' with parameters "
+ parameters;
}

private static void notNull(Object value, String message) {
if (value == null) {
throw new NullPointerException(message);
}
}
}
1 change: 1 addition & 0 deletions src/test/java/org/junit/internal/AllInternalTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@RunWith(Suite.class)
@SuiteClasses({
AnnotatedBuilderTest.class,
ChecksTest.class,
ErrorReportingRunnerTest.class,
ExpectExceptionTest.class,
FailOnTimeoutTest.class,
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/org/junit/internal/ChecksTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.junit.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import static org.junit.internal.Checks.notNull;
import org.junit.Test;

/** Tests for {@link Checks}. */
public class ChecksTest {

@Test
public void notNullShouldReturnNonNullValues() {
Double value = Double.valueOf(3.14);

Double result = notNull(value);

assertSame(value, result);
}

@Test
public void notNullShouldThrowOnNullValues() {
try {
notNull(null);
fail("NullPointerException expected");
} catch (NullPointerException e) {
assertNull("message should be null", e.getMessage());
}
}

@Test
public void notNullWithMessageShouldReturnNonNullValues() {
Float value = Float.valueOf(3.14f);

Float result = notNull(value, "woops");

assertSame(value, result);
}

@Test
public void notNullWithMessageShouldThrowOnNullValues() {
try {
notNull(null, "woops");
fail("NullPointerException expected");
} catch (NullPointerException e) {
assertEquals("message does not match", "woops", e.getMessage());
}
}

@Test
public void notNullWithNullMessageShouldThrowOnNullValues() {
try {
notNull(null, null);
fail("NullPointerException expected");
} catch (NullPointerException e) {
assertNull("message should be null", e.getMessage());
}
}
}