-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Isolation from package-protected spring boot test code (#2)
- Loading branch information
1 parent
44dbbb5
commit b7ff305
Showing
46 changed files
with
222 additions
and
177 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
2 changes: 1 addition & 1 deletion
2
...ork/boot/test/mock/mockito/BeanUtils.java → ...om/teketik/test/mockinbean/BeanUtils.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
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,66 @@ | ||
package com.teketik.test.mockinbean; | ||
|
||
import org.mockito.Mock; | ||
import org.mockito.Spy; | ||
import org.springframework.core.ResolvableType; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* <p>Definition of a {@link Mock mock} or a {@link Spy spy}. | ||
* <p>Corresponding entity can be created using {@link #create(Object)}. | ||
* @author Antoine Meyer | ||
*/ | ||
abstract class Definition { | ||
|
||
protected final String name; | ||
protected final ResolvableType resolvableType; | ||
|
||
Definition(String name, ResolvableType resolvableType) { | ||
super(); | ||
this.name = name; | ||
this.resolvableType = resolvableType; | ||
} | ||
|
||
String getName() { | ||
return name; | ||
} | ||
|
||
ResolvableType getResolvableType() { | ||
return resolvableType; | ||
} | ||
|
||
/** | ||
* Creates a mock or a spy of the provided original value. | ||
* @param <T> | ||
* @param originalValue | ||
* @return | ||
*/ | ||
abstract <T> T create(Object originalValue); | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name, resolvableType); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
Definition other = (Definition) obj; | ||
return Objects.equals(name, other.name) && Objects.equals(resolvableType, other.resolvableType); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Definition [name=" + name + ", resolvableType=" + resolvableType + "]"; | ||
} | ||
|
||
} |
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
22 changes: 22 additions & 0 deletions
22
src/main/java/com/teketik/test/mockinbean/MockDefinition.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,22 @@ | ||
package com.teketik.test.mockinbean; | ||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
import org.mockito.MockSettings; | ||
import org.springframework.boot.test.mock.mockito.MockReset; | ||
import org.springframework.core.ResolvableType; | ||
|
||
class MockDefinition extends Definition { | ||
|
||
MockDefinition(String name, ResolvableType type) { | ||
super(name, type); | ||
} | ||
|
||
@Override | ||
<T> T create(Object originalValue) { | ||
MockSettings settings = MockReset.withSettings(MockReset.AFTER); | ||
settings.name(name); | ||
return (T) mock(resolvableType.resolve(), settings); | ||
} | ||
|
||
} |
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
27 changes: 27 additions & 0 deletions
27
src/main/java/com/teketik/test/mockinbean/SpyDefinition.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,27 @@ | ||
package com.teketik.test.mockinbean; | ||
|
||
import org.mockito.MockSettings; | ||
import org.mockito.Mockito; | ||
import org.springframework.boot.test.mock.mockito.MockReset; | ||
import org.springframework.core.ResolvableType; | ||
import org.springframework.util.Assert; | ||
|
||
class SpyDefinition extends Definition { | ||
|
||
SpyDefinition(String name, ResolvableType type) { | ||
super(name, type); | ||
} | ||
|
||
@Override | ||
<T> T create(Object originalValue) { | ||
Assert.notNull(originalValue, "originalValue must not be null"); | ||
Assert.isInstanceOf(this.resolvableType.resolve(), originalValue); | ||
Assert.state(!Mockito.mockingDetails(originalValue).isSpy(), "originalValue is already a spy"); | ||
MockSettings settings = MockReset.withSettings(MockReset.AFTER); | ||
settings.name(name); | ||
settings.spiedInstance(originalValue); | ||
settings.defaultAnswer(Mockito.CALLS_REAL_METHODS); | ||
return (T) Mockito.mock(originalValue.getClass(), settings); | ||
} | ||
|
||
} |
6 changes: 3 additions & 3 deletions
6
...oot/test/mock/mockito/TestFieldState.java → ...ketik/test/mockinbean/TestFieldState.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
Oops, something went wrong.