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

Parameterized reuses TestClass #1449

Merged
merged 4 commits into from
May 26, 2017

Conversation

kcooney
Copy link
Member

@kcooney kcooney commented May 5, 2017

No description provided.

@kcooney kcooney force-pushed the parameterized-reuses-testclass branch 2 times, most recently from fbbae1e to c6bae86 Compare May 5, 2017 22:07
@kcooney kcooney added the 4.13 label May 5, 2017
@kcooney kcooney requested a review from stefanbirkner May 5, 2017 22:44
@kcooney kcooney force-pushed the parameterized-reuses-testclass branch from e9719c9 to db629a0 Compare May 5, 2017 22:47
@kcooney kcooney force-pushed the parameterized-reuses-testclass branch from db629a0 to 99f4378 Compare May 16, 2017 04:47
@kcooney
Copy link
Member Author

kcooney commented May 23, 2017

@stefanbirkner could you review this?

I would prefer to merge it myself (I split it into multiple commits intentionally)

@stefanbirkner
Copy link
Contributor

I have a look.

@@ -14,6 +14,7 @@
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import org.junit.runners.model.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 import seems not needed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed

*/
public static <T> T notNull(T value, String message) {
if (value == null) {
throw new NullPointerException(String.valueOf(message));
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest passing message directly without String.valueOf().

Copy link
Member Author

Choose a reason for hiding this comment

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

message can be null

Copy link
Contributor

Choose a reason for hiding this comment

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

In this case this method would behave exactly as the one above, similarly to java.util.Objects.requireNonNull()

Copy link
Contributor

Choose a reason for hiding this comment

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

I conform to @panchenko's suggestion. I normally expect that someMethod(someValue) and someMethod(someValue, null) behave the same.

Copy link
Member Author

@kcooney kcooney May 24, 2017

Choose a reason for hiding this comment

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

The behavior I have here matches Guava's Preconditions.checkNotNull(T, String) method. I think the Guava team has given this much more thought than I have, but I can take a stab at explaining why the behavior I have is better.

IMHO it is a programmer error to pass in a null as the second parameter. If we have this method behave like notNull(T) when you pass in a null message we would be ignoring that programmer error. Having the message be "null" makes it clear that 1) someone tried to specify a real message, and 2) they messed up. Hopefully someone would notice and fix it.

Edit: Just checked, and Objects.requireNotNull() just passes the message to the NullPointerException constructor, regardless of whether it is null, so I guess if you guys feel strongly about it I can do that here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, please, lets keep it simple. Also the 1.8 version with Supplier<String> passes the result directly to the NullPointerException constructor.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed notNull(T, String) to not use String.valueOf() on the message.

}
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.

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().

Copy link
Contributor

@stefanbirkner stefanbirkner left a comment

Choose a reason for hiding this comment

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

A few nit-picks only.

*/
public static <T> T notNull(T value, String message) {
if (value == null) {
throw new NullPointerException(String.valueOf(message));
Copy link
Contributor

Choose a reason for hiding this comment

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

I conform to @panchenko's suggestion. I normally expect that someMethod(someValue) and someMethod(someValue, null) behave the same.

@kcooney kcooney force-pushed the parameterized-reuses-testclass branch from 99f4378 to 8755bc1 Compare May 24, 2017 04:40
@kcooney kcooney force-pushed the parameterized-reuses-testclass branch from 8755bc1 to 637b240 Compare May 25, 2017 04:37
kcooney added 4 commits May 25, 2017 09:19
The `BlockJUnit4ClassRunnerWithParameters` created a new instance of
`TestClass` for each parameter set. This led to repeated class
scanning and noticeable memory allocation. Reusing the `TestClass`
avoids theses side effects.

Fixes junit-team#1046.
@kcooney kcooney force-pushed the parameterized-reuses-testclass branch from 637b240 to f34c443 Compare May 25, 2017 16:24
@kcooney
Copy link
Member Author

kcooney commented May 25, 2017

Thanks for the reviews! I rebased and waiting for Travis to complete before doing a merge.

@kcooney kcooney merged commit f34c443 into junit-team:master May 26, 2017
@kcooney kcooney modified the milestone: 4.13 Aug 6, 2017
@kcooney kcooney removed the 4.13 label Aug 6, 2017
@kcooney kcooney deleted the parameterized-reuses-testclass branch August 7, 2017 02:51
sebasjm pushed a commit to sebasjm/junit4 that referenced this pull request Mar 11, 2018
@tasomaniac
Copy link

How does this work when we provide the parameters through constructor? In such cases, the properties in test instance are private final

@panchenko
Copy link
Contributor

@tasomaniac This issue is about a runner for parameterizzed tests - the tests are developed the same way as before.

@tasomaniac
Copy link

I see. The title says reuses TestClass. I thought TestClass is the instance of the test class we create.

aristotle0x01 pushed a commit to aristotle0x01/junit4 that referenced this pull request Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants