-2
编辑:我很抱歉,如果后一直被认为太混乱了,我将修改它,只留下关于我的问题的部分...如何在junit测试中调用包含在实现接口的类中的私有方法?
我已经写了一个名为“ArithmeticNode”类,它实现包含一个接口下面的方法:
public void turnOn() {
arithmeticServer.start();
}
public void turnOff(){
arithmeticServer.stop();
}
,还包含一个私有方法:
private void negotiatePort(NodeManifest nodeManifest, ThriftServer arithmeticServer) {
while(true) {
int proposedPort = arithmeticServer.start();
int returnedPort = managementClient.registerNode(nodeManifest, proposedPort);
if(proposedPOrt != returnedPort) {
arithnemticServer.stop();
}
else
break;
}
}
我试图做的是写在我创建了一个数这些运算节点的测试,让他们注册Ť o我已经编写并用作服务器的管理节点。那么我的项目的第二部分就是让这些节点互动,但这不是实际问题的一部分。 我已经写了一个工作JUnit测试:
@Test
public void testArithmeticServer() throws Exception {
List<NodeManifest> nodeManifests = new ArrayList<>();
nodeManifests.add(new NodeManifest("localhost", Arrays.asList(new String[]{"addition"})));
nodeManifests.add(new NodeManifest("localhost", Arrays.asList(new String[]{"subtraction","multiplication"})));
nodeManifests.add(new NodeManifest("localhost", Arrays.asList(new String[]{"addition","multiplication","division"})));
nodeManifests.add(new NodeManifest("localhost", Arrays.asList(new String[]{"addition","division"})));
List<ThriftServer> arithmeticServers = new ArrayList<>();
for (NodeManifest nodeManifest : nodeManifests) {
ThriftServer arithmeticServer = new ThriftServer(ArithmeticServiceHandler.class);
arithmeticServers.add(arithmeticServer);
negotiatePort(nodeManifest,arithmeticServer);
}
private void negotiatePort() {
while(true) {
int proposedPort = arithmeticServer.start();
int returnedPort = managementClient.registerNode(nodeManifest, proposedPort);
if(proposedPOrt != returnedPort) {
arithnemticServer.stop();
}
else
break;
}
}
我需要编写一个测试,我没有直接把negotiatePort方法测试代码里面,但我呼吁每个节点的私人negotiatePort方法我有我的arithmeticNode类;我不确定是否有可能做到这一点,以及如何做到这一点。我希望我已经比之前版本的帖子少混淆了:-)
http://stackoverflow.com/questions/34571/how-to-test-a-class-that-has-private-methods-fields-or-inner-类?rq = 1 – Manu
您可以使您的negotiatePort方法为private,然后使用[这个如何测试私有方法的答案](http://stackoverflow.com/questions/34571/how-to-test-a-class-that都具有一个-私人的方法字段 - 或内 - 类)。 –
方法不实现接口;类做。 – Raedwald