【发布时间】:2020-12-16 02:38:17
【问题描述】:
我正在尝试为以下代码编写单元测试,但遇到了困难。请帮忙。我不确定如何编写测试,尤其是围绕返回 void 的方法,如 addIdentity() 和 connect()。我正在使用 Mockito 和 powerMockito 框架。
public class A {
public method1() {
//some code here
method2();
//more code here
}
private void method2() {
JSch jsch = new JSch();
Session jschSession = null;
try {
jsch.addIdentity("key");
jschSession = jsch.getSession("userID", "somedomain", 22);
//jsch code
}
}
}
这就是我的测试的样子:
@Test
public void my_Test() throws JSchException {
A test = new A();
JSch mockJsch = Mockito.mock(JSch.class);
whenNew(JSch.class).withNoArguments().thenReturn(mockJsch);
test.method1();
}
【问题讨论】:
-
对这段代码的单元测试应该模拟 JSch 并验证它在不同条件下以您期望的方式调用。
-
我写了这段代码,但是在调用 addIdentity 方法时失败了 mockJsch = Mockito.mock(JSch.class); whenNew(JSch.class).withNoArguments().thenReturn(mockJsch);
-
将您的代码添加到您的问题中,以及确切的错误消息/堆栈跟踪
标签: java unit-testing mockito powermockito