-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Parameterized reuses TestClass #1449
Conversation
fbbae1e
to
c6bae86
Compare
e9719c9
to
db629a0
Compare
db629a0
to
99f4378
Compare
@stefanbirkner could you review this? I would prefer to merge it myself (I split it into multiple commits intentionally) |
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
message
can be null
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()
.
There was a problem hiding this 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)); |
There was a problem hiding this comment.
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.
99f4378
to
8755bc1
Compare
8755bc1
to
637b240
Compare
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.
This avoids repeated class scanning.
637b240
to
f34c443
Compare
Thanks for the reviews! I rebased and waiting for Travis to complete before doing a merge. |
How does this work when we provide the parameters through constructor? In such cases, the properties in test instance are |
@tasomaniac This issue is about a runner for parameterizzed tests - the tests are developed the same way as before. |
I see. The title says reuses TestClass. I thought TestClass is the instance of the test class we create. |
No description provided.