Mockito 3+如何使`mockstatic`返回另一个普通的模拟。

huangapple go评论71阅读模式
英文:

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, but I can't make the static mock return another plain mock and I'm encountering the following 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 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&lt;DriverManager&gt; driverManagerMockedStatic = Mockito.mockStatic(DriverManager.class)) {
            driverManagerMockedStatic.when(
                    () -&gt; 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&lt;DriverManager&gt; driverManagerMockedStatic = Mockito.mockStatic(DriverManager.class)) {
        driverManagerMockedStatic.when(() -&gt; DriverManager.getConnection(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
                .thenAnswer(invocation -&gt; mockedConnection);

        boolean isConnectionOK = sut.testMethod();

        Assertions.assertTrue(isConnectionOK);
    }
}

huangapple
  • 本文由 发表于 2023年5月18日 05:23:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定