gpt4 book ai didi

java - org.powermock.reflect.exceptions.TooManyConstructorsFoundException : Several matching constructors found

转载 作者:行者123 更新时间:2023-12-02 11:25:27 27 4
gpt4 key购买 nike

我已经编写了示例 Kafka Producer 类,我使用本地配置测试了该类,它工作正常。现在我正在尝试编写相应的测试类,因为我正在使用模拟概念的概念,但出现了一些错误

我原来的类(class):-

public class SimpleProducer {

private static final Logger logger = LogManager.getLogger(SimpleProducer.class);
public static String message = null;
public static void main(String[] args) throws Exception {

String topicName = null;
if(args.length == 0){
logger.info("Run class by providing topic-name java -cp /path-of-the-jar/kafka-consumer-producer-jar-with-dependencies.jar com.ewt.SimpleProducer <topic-name> > kafka-consumer.log 2>&1 &");
return;
}
else{
topicName = args[0];
}

// create instance for properties to access producer configs
Properties props = new Properties();

// Assign localhost id
props.put("bootstrap.servers", "localhost:9092");

// Set acknowledgements for producer requests.
props.put("acks", "all");

// If the request fails, the producer can automatically retry,
props.put("retries", 0);

// Specify buffer size in config
props.put("batch.size", 16384);

// Reduce the no of requests less than 0
props.put("linger.ms", 1);

// The buffer.memory controls the total amount of memory available to
// the producer for buffering.
props.put("buffer.memory", 33554432);

props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");

props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

Producer<String, String> producer = new KafkaProducer<String, String>(props);

for (int i = 0; i < 10; i++)
producer.send(new ProducerRecord<String, String>(topicName, Integer.toString(i), Integer.toString(i)));
message = "Messages sent successfully";
logger.info(message);
producer.close();
}
}

测试类:-

import java.util.Properties;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.ewt.SimpleProducer;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Properties.class)
public class SimpleProducerTest {

@Test
public void mainTest() throws Exception{
Properties props = new Properties();
KafkaProducer kafkaProducer = Mockito.mock(KafkaProducer.class);
PowerMockito.whenNew(Properties.class).withNoArguments().thenReturn(props);
PowerMockito.whenNew(KafkaProducer.class).withArguments(Properties.class).thenReturn(kafkaProducer); // showing error here
//PowerMockito.whenNew(KafkaProducer.class).withArguments(props).thenReturn(kafkaProducer);//tried this one also, it is also giving same error
Mockito.doNothing().when(kafkaProducer.send(Mockito.anyObject()));
//Mockito.doNothing().when(kafkaProducer.close());
String args[] = {"test-topic"};
SimpleProducer.main(args);
System.out.println(SimpleProducer.message);
}
}

出现以下错误

org.powermock.reflect.exceptions.TooManyConstructorsFoundException: Several             
matching constructors found, please specify the argument parameter types so that PowerMock can determine which method you're referring to.
Matching constructors in class org.apache.kafka.clients.producer.KafkaProducer were:
org.apache.kafka.clients.producer.KafkaProducer( java.util.Properties.class )
org.apache.kafka.clients.producer.KafkaProducer( java.util.Map.class )

at org.powermock.reflect.internal.WhiteboxImpl.throwExceptionWhenMultipleConstructorMatchesFound(WhiteboxImpl.java:1751)
at org.powermock.reflect.internal.WhiteboxImpl.findUniqueConstructorOrThrowException(WhiteboxImpl.java:1098)
at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.createNewSubstituteMock(DefaultConstructorExpectationSetup.java:98)
at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.withArguments(DefaultConstructorExpectationSetup.java:55)
at com.ewt.test.SimpleProducerTest.mainTest(SimpleProducerTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:117)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

最佳答案

java.util.Properties 是一个 java.util.Map,因此 PowerMock 无法区分这两个构造函数:

org.apache.kafka.clients.producer.KafkaProducer( java.util.Properties.class )

org.apache.kafka.clients.producer.KafkaProducer( java.util.Map.class )

您可以使用 withParameterTypeswithArguments 来准确告诉 PowerMock 您要定位哪个构造函数。

例如:

PowerMockito.whenNew(KafkaProducer.class)
.withParameterTypes(Properties.class)
.withArguments(Properties.class)
.thenReturn(kafkaProducer);

关于java - org.powermock.reflect.exceptions.TooManyConstructorsFoundException : Several matching constructors found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49673221/

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