-
-
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
19 changed files
with
422 additions
and
49 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,18 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* TODO describe interface | ||
* | ||
* @param <T> The type being mocked. | ||
*/ | ||
@Incubating | ||
public interface MockedConstruction<T> extends ScopedMock { | ||
|
||
List<T> allMocks(); | ||
} |
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,24 @@ | ||
/* | ||
* 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 { | ||
|
||
/** | ||
* 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(); | ||
} |
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
/* | ||
* 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> allMocks() { | ||
return Collections.unmodifiableList(control.getMocks()); | ||
} | ||
|
||
@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")); | ||
} | ||
} | ||
} |
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
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.