简体   繁体   中英

Making this JUnit Test pass

Here is the test:

@Test
public void invalidPort() {
    try {
        SS.main(SS_ARGS);
        assertTrue(false);
    } catch (Exception e) {
        assertTrue(true);
    }
}

Here is the relevant code in SS:

public static void main(String[] args) {
    try {

        if (obj.start() == 0) {
            ...stuff
        }
    } catch (BindException e) {
        System.out.println("Address already in use.");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This is what obj.start() does:

public int start() {
    try {
        HttpServer server = HttpServerFactory.create(serverURI);
        this.server = server;
        server.start();
        return 0;
    } catch (NullPointerException e) {
        System.out.println("Error: Please specify the server URI");
        return 1;
    } catch (Exception e) {
        System.out.println("Error: Invalid port!");
        return 1;
    }


}

In this test I am making sure that the port is invalid. When run, the program prints out "Error: Invalid port!" which is good, but the test doesn't pass. It fails because it reaches the assertTrue(false) line. How can I make this test pass?

If you catch all the relevant exceptions in your main method, the main method will not throw an exception and so your test always fails (because it expects an exception to be thrown).

The console output that you mention comes from the catch block of the exception. If you catch the exception, it is gone and will not be passed to the test.

Generally, it is advisable to construct small, testable methods with input and output values. Testing output for the user (like console output) should usually be avoided. By separating the logic as much as possible from the output, one can test thoroughly without having the problems mentioned above.

@Test(expected=IllegalStateException.class)
public void invalidPort() {
        SS.main(SS_ARGS);
}

Replace IllegalStateException with the actual exception implementation that you're expecting. (Perhaps you should be throwing an exception from your main method, rather than just logging it to stderr?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM