gpt4 book ai didi

java - PowerMock Mockito 方法中的模拟类构造函数

转载 作者:太空宇宙 更新时间:2023-11-04 09:02:49 27 4
gpt4 key购买 nike

我在尝试使用 PowerMock 模拟类时遇到问题。首先,这是我当前的 gradle 依赖项:

testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile "org.mockito:mockito-core:2.+"
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.2'
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.2'

我正在尝试模拟对 kafka 经纪人的调用。代码看起来像这样:

public class MyConsumer {
public Consumer<Long, String> createConsumer() {
...
Consumer<Long, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(TOPIC);
....
}
}

createConsumer 中的这两行这就是我试图 mock 的一切。我的测试看起来像这样:

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyConsumer.class)
public class MyConsumerTest {

@Test
public void testCreateConsumer() {
...
KafkaConsumer mockConsumer = mock(KafkaConsumer.class);
doNothing().when(mockConsumer).subscribe(TOPIC);
PowerMockito.whenNew(KafkaConsumer.class).withAnyArguments().thenReturn(mockConsumer);

Consumer<Long, String> result = myConsumerInstance.createConsumer();
...
}
}

当我尝试运行测试时,它们从未开始,并且出现此错误:

> Task :compileTestJava FAILED
/Users/myuser/myproject/src/test/java/myConsumerTest.java:101: error: unreported exception Exception; must be caught or declared to be thrown
PowerMockito.whenNew(KafkaConsumer.class).withAnyArguments().thenReturn(mockConsumer);
^
Note: /Users/myuser/myproject/src/test/java/myConsumerTest.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/myuser/myproject/src/test/java/myConsumerTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

FAILURE: Build failed with an exception.

所以我想我应该将这行代码换行,以尝试获取有关问题所在的更多信息。当我这样做时,测试运行了,但是该特定测试将失败并给出以下错误:

Kafka.analyticsConsumerTest > testCreateConsumer FAILED
java.lang.RuntimeException at PowerMockTestNotifierImpl.java:84
Caused by: org.mockito.exceptions.base.MockitoException at MethodRoadie.java:87

我一生都没弄清楚如何从中提取更多信息。有人遇到过类似的事情吗?

我当前的工作理论与 KafkaConsumer 类如何返回消费者有关?本例中的消费者是 org.apache.kafka.clients.consumer.Consumer 。也许我没有正确地 mock 这一点?任何帮助将不胜感激。

最佳答案

Thanks for the suggestion, but if you reread above, I already did that. That is how I got the Runtime exception to appear. Unless I am misunderstanding what you mean?

也许你这边缺少什么?检查下面的示例,它对我有用。

Gradle :

dependencies {

testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile "org.mockito:mockito-core:2.+"
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.2'
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.2'

compile group: 'org.apache.kafka', name: 'kafka-clients', version: '2.4.0'
}

单元测试:

import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyConsumerTest.MyConsumer.class)
public class MyConsumerTest {

private static final Collection<String> TOPIC = Arrays.asList("1");

static class MyConsumer {

public Consumer<Long, String> createConsumer() {

Properties props = new Properties();
Consumer<Long, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(TOPIC);

return consumer;
}
}

@Test
public void testCreateConsumer() throws Exception {

KafkaConsumer<?,?> mockConsumer = PowerMockito.mock(KafkaConsumer.class);
PowerMockito.doNothing().when(mockConsumer).subscribe(TOPIC); // default behaviour, line is not required
PowerMockito.whenNew(KafkaConsumer.class).withAnyArguments().thenReturn(mockConsumer);

MyConsumer myConsumerInstance = new MyConsumer();
Consumer<Long, String> result = myConsumerInstance.createConsumer();

}
}

关于java - PowerMock Mockito 方法中的模拟类构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60571624/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com