英文:
Mockito 3+ how to make mockstatic return another plain mock
问题
I'm migrating from PowerMockito & MockitoV2 to Mockito V3+ and trying to rewrite my tests.
Previously I had a test which mocked the java.sql.DriverManager class static method getConnection(...) to return a plain Mockito.mock(java.sql.Connection.class). Now I'm trying to do something similar with MockedStatic
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Connection$MockitoMock$sLucRb7f cannot be returned by getConnection()
getConnection() should return Connection
*** If you're unsure why you're getting the above error, read on. Due to the nature of the syntax above, the problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests. Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
My test code looks like this:
@Test
public void testConnectionIsOk() {
Connection mockedConnection = Mockito.mock(Connection.class);
try (MockedStatic<DriverManager> driverManagerMockedStatic = Mockito.mockStatic(DriverManager.class)) {
driverManagerMockedStatic.when(
() -> DriverManager.getConnection(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenReturn(mockedConnection);
boolean isConnectionOK = sut.testMethod();
Assertions.assertTrue(isConnectionOK);
}
}
I understand the error is asking me to use Mockito.doReturn(mockedConnection).when(...)
, but I can't pass a static method call in that .when(...)
block.
Is there any workaround for this use case or am I missing anything?
英文:
I'm migrating from PowerMockito&MockitoV2 to Mockito V3+ and trying to rewrite my tests
Previously I had a test which mocker java.sql.DriverManager class static method getConnection(...) to return a plain Mockito.mock(java.sql.Connection.class), now I'm trying to do somethins similar with MockedStatic<DriverManager> but can't make static mock return another plain mock, have such error:
> org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
> Connection$MockitoMock$sLucRb7f cannot be returned by getConnection()
> getConnection() should return Connection
> *** If you're unsure why you're getting above error read on. Due to the nature of the syntax above problem might occur because:
> 1. This exception might occur in wrongly written multi-threaded tests. Please refer to Mockito FAQ on limitations of concurrency
> testing.
> 2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
> - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
My test code looks like that:
@Test
public void testConnectionIsOk() {
Connection mockedConnection = Mockito.mock(Connection.class);
try (MockedStatic<DriverManager> driverManagerMockedStatic = Mockito.mockStatic(DriverManager.class)) {
driverManagerMockedStatic.when(
() -> DriverManager.getConnection(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenReturn(mockedConnection);
boolean isConnectionOK = sut.testMethod();
Assertions.assertTrue(isConnectionOK);
}
}
I understand the error is asking me to use a Mockito.doReturn(mockedConnection).when(...)
but I can't pass a static method call in that .when(...)
block
Is there any workaround for this use case or am I missing anything?
答案1
得分: 0
你可以使用 thenAnswer
替代 thenReturn
,并且只需返回 lambda 表达式,就像这样:
@Test
public void testConnectionIsOk() {
Connection mockedConnection = Mockito.mock(Connection.class);
try (MockedStatic<DriverManager> driverManagerMockedStatic = Mockito.mockStatic(DriverManager.class)) {
driverManagerMockedStatic.when(() -> DriverManager.getConnection(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenAnswer(invocation -> mockedConnection);
boolean isConnectionOK = sut.testMethod();
Assertions.assertTrue(isConnectionOK);
}
}
英文:
You can use use thenAnswer
instead of thenReturn
and just return lambda expression like:
@Test
public void testConnectionIsOk() {
Connection mockedConnection = Mockito.mock(Connection.class);
try (MockedStatic<DriverManager> driverManagerMockedStatic = Mockito.mockStatic(DriverManager.class)) {
driverManagerMockedStatic.when(() -> DriverManager.getConnection(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenAnswer(invocation -> mockedConnection);
boolean isConnectionOK = sut.testMethod();
Assertions.assertTrue(isConnectionOK);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论