Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring JUnit rule implementation
Needed for work on Mockito strictness, see #769. Decoupled JUnit API from the work Mockito does before and after every test. No public API exposed, listeners and friends need to be refactored and documented. Don't merge. This refactoring on its own does not add value.
- Loading branch information
1 parent
613977a
commit 53ea392
Showing
4 changed files
with
117 additions
and
55 deletions.
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
10 changes: 10 additions & 0 deletions
10
src/main/java/org/mockito/internal/junit/MockitoTestListener.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,10 @@ | ||
package org.mockito.internal.junit; | ||
|
||
import java.util.Collection; | ||
|
||
interface MockitoTestListener { | ||
|
||
void beforeTest(Object testClassInstance, String testMethodName); | ||
|
||
void afterTest(Collection<Object> mocks, Throwable problem); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/mockito/internal/junit/SilentTestListener.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,19 @@ | ||
package org.mockito.internal.junit; | ||
|
||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.util.Collection; | ||
|
||
class SilentTestListener implements MockitoTestListener { | ||
public void beforeTest(Object testClassInstance, String testMethodName) { | ||
MockitoAnnotations.initMocks(testClassInstance); | ||
} | ||
|
||
public void afterTest(Collection<Object> mocks, Throwable problem) { | ||
if (problem == null) { | ||
//Validate only when there is no other problem to avoid reporting multiple problems | ||
Mockito.validateMockitoUsage(); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/org/mockito/internal/junit/WarningTestListener.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,36 @@ | ||
package org.mockito.internal.junit; | ||
|
||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.internal.util.MockitoLogger; | ||
|
||
import java.util.Collection; | ||
|
||
class WarningTestListener implements MockitoTestListener { | ||
|
||
private final MockitoLogger logger; | ||
private String testName; | ||
|
||
WarningTestListener(MockitoLogger logger) { | ||
this.logger = logger; | ||
} | ||
|
||
public void beforeTest(Object testClassInstance, String testMethodName) { | ||
testName = testClassInstance.getClass().getSimpleName() + "." + testMethodName; | ||
MockitoAnnotations.initMocks(testClassInstance); | ||
} | ||
|
||
public void afterTest(Collection<Object> mocks, Throwable problem) { | ||
if (problem != null) { | ||
//print stubbing mismatches only when there is a test failure | ||
//to avoid false negatives. Give hint only when test fails. | ||
new ArgMismatchFinder().getStubbingArgMismatches(mocks).format(testName, logger); | ||
} else { | ||
//Validate only when there is no other problem to avoid reporting multiple problems | ||
Mockito.validateMockitoUsage(); | ||
|
||
//print unused stubbings only when test succeeds to avoid reporting multiple problems and confusing users | ||
new UnusedStubbingsFinder().getUnusedStubbings(mocks).format(testName, logger); | ||
} | ||
} | ||
} |