Fix compatibility with Mockito 2.0
Update MockDefinition to only call `Answers.get()` if a direct cast isn't possible. This provides compatibility with Mockito 2.0 (currently 2.0.47-beta) without breaking support for Mockto 1.x. Closes gh-6323
This commit is contained in:
committed by
Phillip Webb
parent
3286760073
commit
ed829d7981
@@ -24,6 +24,7 @@ import java.util.Set;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.MockSettings;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -151,11 +152,19 @@ class MockDefinition extends Definition {
|
||||
if (!this.extraInterfaces.isEmpty()) {
|
||||
settings.extraInterfaces(this.extraInterfaces.toArray(new Class<?>[] {}));
|
||||
}
|
||||
settings.defaultAnswer(this.answer.get());
|
||||
settings.defaultAnswer(getAnswer(this.answer));
|
||||
if (this.serializable) {
|
||||
settings.serializable();
|
||||
}
|
||||
return (T) Mockito.mock(this.classToMock, settings);
|
||||
}
|
||||
|
||||
private Answer<?> getAnswer(Answers answer) {
|
||||
if (Answer.class.isInstance(answer)) {
|
||||
// With Mockito 2.0 we can directly cast the answer
|
||||
return (Answer<?>) ((Object) answer);
|
||||
}
|
||||
return answer.get();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user