-2

edit: I apologize if the post has been considered too confusing, i'll edit it and leave only the parts regarding my problem...

I have written a class named "ArithmeticNode" which implements an interface containing the following methods:

public void turnOn() {
  arithmeticServer.start();
} 

public void turnOff(){
  arithmeticServer.stop();
}

and contains also a private method:

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;
    }
}

What I'm trying to do is to write a test in which I create a number of these arithmetic nodes and make them register to a management node that I've already written and use as server. Then there will be a second part of my project where I'll make those nodes interact, but that's not part of the actual problem. I have already written a working junit test:

@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;
    }
}

I need to write a test where i don't have to put the negotiatePort method directly inside the test code, but I call for each node the private negotiatePort method I have inside my arithmeticNode class; I'm not sure if it's possible to do that and how. I hope I've been less confusing than the previous version of the post :-)

Gaspare79
  • 51
  • 1
  • 9
  • http://stackoverflow.com/questions/34571/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes?rq=1 – Manu Feb 12 '16 at 11:31
  • You can make your negotiatePort method private and then use [this answer of how to test private methods](http://stackoverflow.com/questions/34571/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes). – Krasimir Stoev Feb 12 '16 at 11:47
  • Methods do not implement interfaces; classes do. – Raedwald Feb 12 '16 at 12:02
  • Sorry but, where did I write the contrary? I said I have created an interface that contains three methods, and a class that implements those methods. I probably put it in a not perfect way, but I've never said that methods implements an interface... I'll take a look at the answer linked in previous posts, thanks for the hint – Gaspare79 Feb 12 '16 at 13:39
  • I'm sure you can remove 90% of that text and **leave just enough to describe the real problem**. All the other fluff around is only confusing. – JensG Feb 12 '16 at 17:08
  • Sorry, I though that it would've been correct to describe a bit what i'm doing before asking directly about the problem... I'll edit the post if it's too confusing I apologize for that. – Gaspare79 Feb 12 '16 at 17:11

1 Answers1

1

You can make the negotiatePort method package local. Then you would be able to call it within your test (which should reside in the same package). And nobody outside the package would be able to use it