-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements mocking for constructor invocations.
- Loading branch information
Showing
28 changed files
with
1,125 additions
and
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.util.List; | ||
|
||
/** | ||
* Represents a mock of any object construction of the represented type. Within the scope of the | ||
* mocked construction, the invocation of any interceptor will generate a mock which will be | ||
* prepared as specified when generating this scope. The mock can also be received via this | ||
* instance. | ||
* <p> | ||
* If the {@link Mock} annotation is used on fields or method parameters of this type, a mocked | ||
* construction is created instead of a regular mock. The mocked construction is activated and | ||
* released upon completing any relevant test. | ||
* | ||
* @param <T> The type for which the construction is being mocked. | ||
*/ | ||
@Incubating | ||
public interface MockedConstruction<T> extends ScopedMock { | ||
|
||
List<T> constructed(); | ||
|
||
interface Context { | ||
|
||
int getCount(); | ||
|
||
Constructor<?> constructor(); | ||
|
||
List<?> arguments(); | ||
} | ||
|
||
interface MockInitializer<T> { | ||
|
||
void prepare(T mock, Context context) 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito; | ||
|
||
/** | ||
* Represents a mock with a thread-local explicit scope. Scoped mocks must be closed by the entity | ||
* that activates the scoped mock. | ||
*/ | ||
@Incubating | ||
public interface ScopedMock extends AutoCloseable { | ||
|
||
/** | ||
* Checks if this mock is closed. | ||
* | ||
* @return {@code true} if this mock is closed. | ||
*/ | ||
boolean isClosed(); | ||
|
||
/** | ||
* Closes this scoped mock and throws an exception if already closed. | ||
*/ | ||
@Override | ||
void close(); | ||
|
||
/** | ||
* Releases this scoped mock and is non-operational if already released. | ||
*/ | ||
void closeOnDemand(); | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/org/mockito/internal/MockedConstructionImpl.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,64 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito.internal; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.mockito.MockedConstruction; | ||
import org.mockito.exceptions.base.MockitoException; | ||
import org.mockito.internal.debugging.LocationImpl; | ||
import org.mockito.invocation.Location; | ||
import org.mockito.plugins.MockMaker; | ||
|
||
import static org.mockito.internal.util.StringUtil.*; | ||
|
||
public final class MockedConstructionImpl<T> implements MockedConstruction<T> { | ||
|
||
private final MockMaker.ConstructionMockControl<T> control; | ||
|
||
private boolean closed; | ||
|
||
private final Location location = new LocationImpl(); | ||
|
||
protected MockedConstructionImpl(MockMaker.ConstructionMockControl<T> control) { | ||
this.control = control; | ||
} | ||
|
||
@Override | ||
public List<T> constructed() { | ||
return Collections.unmodifiableList(control.getMocks()); | ||
} | ||
|
||
@Override | ||
public boolean isClosed() { | ||
return closed; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
assertNotClosed(); | ||
|
||
closed = true; | ||
control.disable(); | ||
} | ||
|
||
@Override | ||
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")); | ||
} | ||
} | ||
} |
Oops, something went wrong.