英文:
@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<HttpRequest> request;
MockedStatic<HttpClient> staticClient;
public MockedStatic<HttpClient> 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<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"));
}
}
My dependencies are:
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'
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论