我正在尝试设置单元测试,以便可以测试hbase客户端。但是,在构造函数中模拟创建到hbase的连接时遇到了问题。我认为我没有正确地将模拟连接注入到我要测试的类中,但是我不确定我在哪里犯了错误。我已经研究过关于模拟连接的类似问题,但是所有这些问题都是在构造函数之外创建连接的。
这是我要测试的代码:
@Lazy
@Service("HBaseClient")
public class HBaseClient {
/**
* Instantiate a new client and create connection to HBase.
*/
public HBaseClient() {
// Create connection to HBase
conf = HBaseConfiguration.create();
conf.setInt("timeout", 120000);
conf.set("hbase.zookeeper.quorum", zookeeperHost);
conf.set("hbase.zookeeper.property.clientPort", zookeeperPort);
conf.set("zookeeper.znode.parent", znodeParent);
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
logger.error("Error creating connection to HBase - IOException");
}
}
public void addRecord(String rowKey, String columnFamily, Map<String, String> values) {
...
下面是我在单元测试中得到的:
@RunWith(PowerMockRunner.class)
public class TestHBaseClient {
@InjectMocks
private static HBaseClient hbaseClient;
@BeforeClass
@PrepareForTest({ConnectionFactory.class})
public static void setUpBeforeClass() throws Exception {
Connection mockConnection = PowerMockito.mock(Connection.class);
PowerMockito.mockStatic(ConnectionFactory.class);
PowerMockito.when(ConnectionFactory.createConnection()).thenReturn(mockConnection);
hbaseClient = new HBaseClient();
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void testAddRecord() {
HashMap<String, String> values = new HashMap<String, String>();
values.put("test1", "abc");
values.put("test2", "xyz");
hbaseClient.addRecord("test", "Test", values);
}
}
当前的代码引发以下错误:
org.mockito.exceptions.misusing.missingmethodinvocationexception:when()要求参数必须是“模拟的方法调用”。例如:when(mock.getarticles()).thenreturn(articles);
此外,出现此错误的原因可能是:1。您可以存根其中一个:final/private/equals()/hashcode()方法。这些方法无法存根/验证。2在when()中,您不在mock上调用方法,而是在其他对象上调用方法。三。模拟类的父级不是公共的。这是模拟引擎的一个限制。
在org.powermock.api.mockito.powermockito.when(powermockito。java:495)在com.rtn.cdp.storage.hbase.test.testhbasetaclient.setupbeforeclass(testhbaseclient。java:32)在sun.reflect.nativemethodaccessorimpl.invoke0(本机方法)在sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl)。java:62)在sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl。java:43)在java.lang.reflect.method.invoke(方法。java:498)在org.junit.internal.runners.classroadie.runbefores(classroadie。java:56)在org.junit.internal.runners.classroadie.runprotected(classroadie。java:43)在org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateinpl.run(powermockjunit44runnerdelegateinpl。java:118)在org.powermock.modules.junit4.common.internal.impl.junit4testsuitechunkerimpl.run(junit4testsuitechunkerimpl。java:101)在org.powermock.modules.junit4.common.internal.impl.abstractcommonpowermockrunner.run(abstractcommonpowermockrunner)。java:53)在org.powermock.modules.junit4.powermockrunner.run(powermockrunner。java:53)位于org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference)。java:86)在org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution。java:38)位于org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner)。java:459)位于org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner)。java:675)在org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner。java:382)在org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner。java:192)
暂无答案!
目前还没有任何答案,快来回答吧!