@Captor和@Mock在使用MockitoJUnitRunner时未创建对象。

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

@Captor and @Mock not creating objects with MockitoJUnitRunner

问题

我正在尝试使用 @Mock@Captor 注解编写测试。然而,对象没有被创建,所以我得到了 NullPointerExceptions

测试代码:

@RunWith(MockitoJUnitRunner.class)
public class ServiceTest {
    @Mock
    HttpClient.Builder builder;
    @Mock
    HttpClient client;
    @Captor
    ArgumentCaptor<HttpRequest> request;

    MockedStatic<HttpClient> staticClient;

    public MockedStatic<HttpClient> server() {
        // 设置模拟服务器
        staticClient = Mockito.mockStatic(HttpClient.class);
        staticClient.when(HttpClient::newBuilder).thenReturn(builder);
        when(builder.build()).thenReturn(client);
        return staticClient;
    }

    @Test
    public void shouldCallService() throws IOException, InterruptedException {
        try (MockedStatic<HttpClient> ignored = server()) {
            HttpResponse response = mock(HttpResponse.class);
            when(client.send(any(), any())).thenReturn(response);
            when(response.body()).thenReturn("response");

            TestService service = new TestService();
            service.callService();

            verify(client).send(captor.capture(), HttpResponse.BodyHandlers.ofString());
            assertThat(captor.getValue().uri().getPath(), equalTo("localhost:8081"));
        }
    }
}

我的依赖项:

implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.mockito:mockito-core:3.5.11'
testImplementation "org.mockito:mockito-inline"
testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.mockito', module: 'mockito-core'
}
英文:

I am trying to write tests with the @Mock and @Captor annotations. However, the objects are not being created so I get NullPointerExceptions.

The test:

@RunWith(MockitoJUnitRunner.class)
public class ServiceTest {
    @Mock
    HttpClient.Builder builder;
    @Mock
    HttpClient client;
    @Captor
    ArgumentCaptor&lt;HttpRequest&gt; request;

    MockedStatic&lt;HttpClient&gt; staticClient;

    public MockedStatic&lt;HttpClient&gt; server() {
        // Set up mock server
        staticClient= Mockito.mockStatic(HttpClient.class);
        staticClient.when(HttpClient::newBuilder).thenReturn(builder);
        when(builder.build()).thenReturn(client);
        return staticClient;
    }

    @Test
    public void shouldCallService() throws IOException, InterruptedException {
        try (MockedStatic&lt;HttpClient&gt; ignored = server()) {
            HttpResponse response = mock(HttpResponse.class);
            when(client.send(any(), any())).thenReturn(response);
            when(response.body()).thenReturn(&quot;response&quot;);

            TestService service = new TestService();
            service.callService();

            verify(client).send(captor.capture(), HttpResponse.BodyHandlers.ofString());
            assertThat(captor.getValue().uri().getPath(), equalTo(&quot;localhost:8081&quot;));
        }
}

My dependencies are:

implementation &#39;org.springframework.boot:spring-boot-starter-web&#39;
testImplementation &#39;org.mockito:mockito-core:3.5.11&#39;
testImplementation &quot;org.mockito:mockito-inline&quot;
testImplementation(&#39;org.springframework.boot:spring-boot-starter-test&#39;) {
	exclude group: &#39;org.mockito&#39;, module: &#39;mockito-core&#39;
}

答案1

得分: 2

我怀疑你可能正在使用JUnit 5来运行你的测试,如果是这种情况,那么@RunWith注解不起作用。尝试使用@ExtendWith(MockitoExtension.class)代替。

英文:

I suspect you might be running your tests with JUnit 5, in which case the @RunWith annotation doesn't work. Try using @ExtendWith(MockitoExtension.class)
instead.

答案2

得分: 0

可能与您的依赖关系相关

您能否更新,仅使用spring-boot-starter-test并删除所有mockito

英文:

Maybe related to your dependencies

Can you update to use only spring-boot-starter-test and remove all mockito

huangapple
  • 本文由 发表于 2020年9月26日 20:42:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64077805.html
匿名

发表评论

匿名网友

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

确定