Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mock resolver API to allow for resolving provided instances from …
…third-party frameworks.
- Loading branch information
Showing
12 changed files
with
229 additions
and
5 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
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
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,22 @@ | ||
/* | ||
* Copyright (c) 2020 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito.plugins; | ||
|
||
/** | ||
* A mock resolver offers an opportunity to resolve a mock from any instance that is | ||
* provided to the {@link org.mockito.Mockito}-DSL. This mechanism can be used by | ||
* frameworks that provide mocks that are implemented by Mockito but which are wrapped | ||
* by other instances to enhance the proxy further. | ||
*/ | ||
public interface MockResolver { | ||
|
||
/** | ||
* Returns the provided instance or the unwrapped mock that the provided | ||
* instance represents. This method must not return {@code null}. | ||
* @param instance The instance passed to the {@link org.mockito.Mockito}-DSL. | ||
* @return The provided instance or the unwrapped mock. | ||
*/ | ||
Object resolve(Object instance); | ||
} |
50 changes: 50 additions & 0 deletions
50
subprojects/extTest/src/test/java/org/mockitousage/plugins/resolver/MockResolverTest.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,50 @@ | ||
/* | ||
* Copyright (c) 2020 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockitousage.plugins.resolver; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.mockito.junit.jupiter.MockitoSettings; | ||
import org.mockito.quality.Strictness; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.*; | ||
|
||
@MockitoSettings(strictness = Strictness.WARN) | ||
@ExtendWith(MockitoExtension.class) | ||
class MockResolverTest { | ||
|
||
@Test | ||
void mock_resolver_can_unwrap_mocked_instance() { | ||
Foo mock = mock(Foo.class), wrapper = new MockWrapper(mock); | ||
when(wrapper.doIt()).thenReturn(123); | ||
assertThat(mock.doIt()).isEqualTo(123); | ||
verify(wrapper).doIt(); | ||
} | ||
|
||
interface Foo { | ||
int doIt(); | ||
} | ||
|
||
static class MockWrapper implements Foo { | ||
|
||
private final Foo mock; | ||
|
||
MockWrapper(Foo mock) { | ||
this.mock = mock; | ||
} | ||
|
||
Object getMock() { | ||
return mock; | ||
} | ||
|
||
@Override | ||
public int doIt() { | ||
return mock.doIt(); | ||
} | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
subprojects/extTest/src/test/java/org/mockitousage/plugins/resolver/MyMockResolver.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,18 @@ | ||
/* | ||
* Copyright (c) 2020 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockitousage.plugins.resolver; | ||
|
||
import org.mockito.plugins.MockResolver; | ||
|
||
public class MyMockResolver implements MockResolver { | ||
|
||
@Override | ||
public Object resolve(Object instance) { | ||
if (instance instanceof MockResolverTest.MockWrapper) { | ||
return ((MockResolverTest.MockWrapper) instance).getMock(); | ||
} | ||
return instance; | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
subprojects/extTest/src/test/resources/mockito-extensions/org.mockito.plugins.MockResolver
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 @@ | ||
org.mockitousage.plugins.resolver.MyMockResolver |