Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mockito \#1013: Defines and implements API for static mocking.
- Loading branch information
Showing
37 changed files
with
1,046 additions
and
88 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
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
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,168 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito; | ||
|
||
import org.mockito.exceptions.base.MockitoAssertionError; | ||
import org.mockito.exceptions.base.MockitoException; | ||
import org.mockito.internal.creation.StaticMockControl; | ||
import org.mockito.internal.debugging.LocationImpl; | ||
import org.mockito.internal.listeners.VerificationStartedNotifier; | ||
import org.mockito.internal.progress.MockingProgress; | ||
import org.mockito.internal.stubbing.InvocationContainerImpl; | ||
import org.mockito.internal.verification.MockAwareVerificationMode; | ||
import org.mockito.internal.verification.VerificationDataImpl; | ||
import org.mockito.invocation.Location; | ||
import org.mockito.invocation.MockHandler; | ||
import org.mockito.stubbing.OngoingStubbing; | ||
import org.mockito.verification.VerificationMode; | ||
|
||
import static org.mockito.Mockito.times; | ||
import static org.mockito.internal.exceptions.Reporter.missingMethodInvocation; | ||
import static org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress; | ||
import static org.mockito.internal.util.MockUtil.getInvocationContainer; | ||
import static org.mockito.internal.util.MockUtil.resetMock; | ||
import static org.mockito.internal.util.StringUtil.join; | ||
import static org.mockito.internal.verification.VerificationModeFactory.noInteractions; | ||
import static org.mockito.internal.verification.VerificationModeFactory.noMoreInteractions; | ||
|
||
@Incubating | ||
public class MockedStatic<T> implements AutoCloseable { | ||
|
||
private final StaticMockControl<T> control; | ||
|
||
private boolean closed; | ||
|
||
private final Location location = new LocationImpl(); | ||
|
||
public MockedStatic(StaticMockControl<T> control) { | ||
this.control = control; | ||
} | ||
|
||
public <S> OngoingStubbing<S> when(Verification verification) { | ||
assertNotClosed(); | ||
|
||
try { | ||
verification.apply(); | ||
} catch (Throwable ignored) { } | ||
|
||
MockingProgress mockingProgress = mockingProgress(); | ||
mockingProgress.stubbingStarted(); | ||
@SuppressWarnings("unchecked") | ||
OngoingStubbing<S> stubbing = (OngoingStubbing<S>) mockingProgress.pullOngoingStubbing(); | ||
if (stubbing == null) { | ||
mockingProgress.reset(); | ||
throw missingMethodInvocation(); | ||
} | ||
return stubbing; | ||
} | ||
|
||
public void verify(Verification verification) { | ||
verify(times(1), verification); | ||
} | ||
|
||
public void verify(VerificationMode mode, Verification verification) { | ||
assertNotClosed(); | ||
|
||
MockingDetails mockingDetails = Mockito.mockingDetails(control.getType()); | ||
MockHandler handler = mockingDetails.getMockHandler(); | ||
|
||
VerificationStartedNotifier.notifyVerificationStarted( | ||
handler.getMockSettings().getVerificationStartedListeners(), | ||
mockingDetails); | ||
|
||
MockingProgress mockingProgress = mockingProgress(); | ||
VerificationMode actualMode = mockingProgress.maybeVerifyLazily(mode); | ||
mockingProgress.verificationStarted( | ||
new MockAwareVerificationMode( | ||
control.getType(), actualMode, mockingProgress.verificationListeners())); | ||
|
||
try { | ||
verification.apply(); | ||
} catch (MockitoException | MockitoAssertionError e) { | ||
throw e; | ||
} catch (Throwable t) { | ||
throw new MockitoException(join( | ||
"An unexpected error occurred while verifying a static stub", | ||
"", | ||
"To correctly verify a stub, invoke a single static method of " + control.getType().getName() + " in the provided lambda.", | ||
"For example, if a method 'sample' was defined, provide a lambda or anonymous class containing the code", | ||
"", | ||
"() -> " + control.getType().getSimpleName() + ".sample()", | ||
"or", | ||
control.getType().getSimpleName() + "::sample" | ||
), t); | ||
} | ||
} | ||
|
||
public void reset() { | ||
assertNotClosed(); | ||
|
||
MockingProgress mockingProgress = mockingProgress(); | ||
mockingProgress.validateState(); | ||
mockingProgress.reset(); | ||
mockingProgress.resetOngoingStubbing(); | ||
|
||
resetMock(control.getType()); | ||
} | ||
|
||
public void clearInvocations() { | ||
assertNotClosed(); | ||
|
||
MockingProgress mockingProgress = mockingProgress(); | ||
mockingProgress.validateState(); | ||
mockingProgress.reset(); | ||
mockingProgress.resetOngoingStubbing(); | ||
|
||
getInvocationContainer(control.getType()).clearInvocations(); | ||
} | ||
|
||
public void verifyNoMoreInteractions() { | ||
assertNotClosed(); | ||
|
||
mockingProgress().validateState(); | ||
InvocationContainerImpl invocations = getInvocationContainer(control.getType()); | ||
VerificationDataImpl data = new VerificationDataImpl(invocations, null); | ||
noMoreInteractions().verify(data); | ||
} | ||
|
||
public void verifyNoInteractions() { | ||
assertNotClosed(); | ||
|
||
mockingProgress().validateState(); | ||
InvocationContainerImpl invocations = getInvocationContainer(control.getType()); | ||
VerificationDataImpl data = new VerificationDataImpl(invocations, null); | ||
noInteractions().verify(data); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
assertNotClosed(); | ||
|
||
closed = true; | ||
control.disable(); | ||
} | ||
|
||
public void closeOnDemand() { | ||
if (!closed) { | ||
close(); | ||
} | ||
} | ||
|
||
private void assertNotClosed() { | ||
if (closed) { | ||
throw new MockitoException(join( | ||
"The static mock created at", | ||
location.toString(), | ||
"is already resolved and cannot longer be used" | ||
)); | ||
} | ||
} | ||
|
||
@FunctionalInterface | ||
public interface Verification { | ||
|
||
void apply() throws Throwable; | ||
} | ||
} |
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
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
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
Oops, something went wrong.