我有一个 PowerMockRunner 的测试用例。在Junit中,我需要与一些SecureClient建立连接。
但是,如果我添加'@RunWith(PowerMockRunner.class)'然后运行JUnit,则会导致SecureClient CommunicationException ,但是
如果我从Junit中删除“ @RunWith(PowerMockRunner.class)”,然后 运行JUnit,连接成功。
问题是我无法删除PowerMockRunner,因为有许多依赖它的现有测试(此处未提及)。
这是Junit代码-
@RunWith(PowerMockRunner.class)
@PowerMockIgnore( {"javax.management.*"})
public class ClassATest {
ClassA classA;
@BeforeClass
public static void beforeclass() throws Exception {
ClassB.methodB();
}
@Before
public void before() {
classA = Mockito.mock(ClassA.class, Mockito.CALLS_REAL_METHODS);
}
@Test
public void test() {
// Some random test
assert("String".equals("String"));
}
}
ClassB-
public class ClassB {
public static void methodB() throws IOException{
secureClient secureClient = ConnectionClass.getInstance();
}
}
发生连接的类,但是如果在ClassA中使用PowerMockRunner则会中断-
public class ConnectionClass {
// SecureClient is a private Class.
private static SecureClient secureClient;
public static SecureClient getInstance() throws IOException {
Properties secureProperties = new Properties();
secureProperties.put("yyy", "xxxx");
secureProperties.put("yyy", "xxxx");
secureProperties.put("yyy", "xxxx");
secureClient = SecureClient.Factory.newInstance(secureProperties);
return secureClient;
}
}
我无法弄清楚为什么PowerMockRunner会阻止连接,并且解决该问题的方法可能是什么?在进行任何模拟或PowerMockRunner对其产生影响之前,是否可以在@BeforeClass注释块中运行代码?