mockito方法中的java-mocking类构造函数

643ylb08  于 2021-06-05  发布在  Kafka
关注(0)|答案(1)|浏览(555)

我在尝试使用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 都是我想嘲笑的。我的测试是这样的:

@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

我一辈子都不知道如何从中提取更多的信息。以前有人遇到过这样的事情吗?
我目前的工作理论是关于Kafka消费阶层是如何返回消费者的?此示例中的使用者是 org.apache.kafka.clients.consumer.Consumer . 也许我没有正确地嘲笑这一点?任何协助都将不胜感激。

szqfcxe2

szqfcxe21#

谢谢你的建议,但如果你重读以上,我已经做了。我就是这样得到运行时异常的。除非我误解了你的意思?
也许你那边少了什么?检查下面的例子,它为我工作。
grad尔:

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();

    }
}

相关问题