2016-02-12 57 views
-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類;我不確定是否有可能做到這一點,以及如何做到這一點。我希望我已經比之前版本的帖子少混淆了:-)

+0

http://stackoverflow.com/questions/34571/how-to-test-a-class-that-has-private-methods-fields-or-inner-類?rq = 1 – Manu

+0

您可以使您的negotiatePort方法爲private,然後使用[這個如何測試私有方法的答案](http://stackoverflow.com/questions/34571/how-to-test-a-class-that都具有一個-私人的方法字段 - 或內 - 類)。 –

+0

方法不實現接口;類做。 – Raedwald

回答

1

您可以使negotiatePort方法包本地。然後你可以在你的測試中調用它(應該放在同一個包中)。沒有人能夠使用它的包裹

相關問題