-
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
Ensure exceptions from methodBlock() don't result in unrooted tests #1082
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/test/java/org/junit/runners/CustomBlockJUnit4ClassRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.runners; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.junit.runner.Description; | ||
import org.junit.runner.notification.Failure; | ||
import org.junit.runner.notification.RunListener; | ||
import org.junit.runner.notification.RunNotifier; | ||
import org.junit.runners.model.FrameworkMethod; | ||
import org.junit.runners.model.InitializationError; | ||
import org.junit.runners.model.Statement; | ||
|
||
/** | ||
* Tests that verify proper behavior for custom runners that extend | ||
* {@link BlockJUnit4ClassRunner}. | ||
* | ||
* @author Sam Brannen | ||
* @since 4.13 | ||
*/ | ||
public class CustomBlockJUnit4ClassRunnerTest { | ||
|
||
@Test | ||
public void exceptionsFromMethodBlockMustNotResultInUnrootedTests() throws Exception { | ||
TrackingRunListener listener = new TrackingRunListener(); | ||
RunNotifier notifier = new RunNotifier(); | ||
notifier.addListener(listener); | ||
|
||
new CustomBlockJUnit4ClassRunner(CustomBlockJUnit4ClassRunnerTestCase.class).run(notifier); | ||
assertEquals("tests started.", 2, listener.testStartedCount.get()); | ||
assertEquals("tests failed.", 1, listener.testFailureCount.get()); | ||
assertEquals("tests finished.", 2, listener.testFinishedCount.get()); | ||
} | ||
|
||
|
||
@Ignore("This test case is run manually by the enclosing test class") | ||
public static class CustomBlockJUnit4ClassRunnerTestCase { | ||
@Test public void shouldPass() { /* no-op */ } | ||
@Test public void throwException() { /* no-op */ } | ||
} | ||
|
||
/** | ||
* Custom extension of {@link BlockJUnit4ClassRunner} that always throws | ||
* an exception from the {@code methodBlock()} if a test method is named | ||
* exactly {@code "throwException"}. | ||
*/ | ||
private static class CustomBlockJUnit4ClassRunner extends BlockJUnit4ClassRunner { | ||
|
||
CustomBlockJUnit4ClassRunner(Class<?> testClass) throws InitializationError { | ||
super(testClass); | ||
} | ||
|
||
@Override | ||
protected Statement methodBlock(FrameworkMethod method) { | ||
if ("throwException".equals(method.getName())) { | ||
throw new RuntimeException("throwException() test method invoked"); | ||
} | ||
return super.methodBlock(method); | ||
} | ||
} | ||
|
||
/** | ||
* Simple {@link RunListener} that tracks the number of times that | ||
* certain callbacks are invoked. | ||
*/ | ||
private static class TrackingRunListener extends RunListener { | ||
|
||
final AtomicInteger testStartedCount = new AtomicInteger(); | ||
final AtomicInteger testFailureCount = new AtomicInteger(); | ||
final AtomicInteger testFinishedCount = new AtomicInteger(); | ||
|
||
|
||
@Override | ||
public void testStarted(Description description) throws Exception { | ||
testStartedCount.incrementAndGet(); | ||
} | ||
|
||
@Override | ||
public void testFailure(Failure failure) throws Exception { | ||
testFailureCount.incrementAndGet(); | ||
} | ||
|
||
@Override | ||
public void testFinished(Description description) throws Exception { | ||
testFinishedCount.incrementAndGet(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't think you need the
@Ignore
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.
Define "need". ;)
When executing tests via
org.junit.tests.AllTests
, you are correct: the@Ignore
is unnecessary.However, when executing tests within an IDE such as Eclipse, it is very useful to have such internal test classes disabled by default. In that manner, there are no false negatives when executing all tests within a given package, class, etc. Otherwise, such tests that always fail (intentionally) will produce undesirable noise and distract developers from the task at hand.
For the concrete case of
CustomBlockJUnit4ClassRunnerTestCase
, all test methods within that class are in fact no-ops and would therefore never fail if executed as is; however, having additional passing tests that provide zero value is well... of zero value.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.
When I run tests in Eclipse, it doesn't run nested classes. We use nested classes in many of our tests, and I haven't seen us use
@Ignore
(and, in fact, some of the tests depend on there not being@Ignore
present)