Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I'm unable to debug my code in TestNG

I am running automation tests using TestNG and Java.

Here is part of my code:

private void testConnection(String URL1) throws IOException {

    try {
        URL url = new URL(URL1);
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
        urlConn.connect();

        assertEquals(HttpURLConnection.HTTP_OK, urlConn.getResponseCode());
    } catch (IOException e) {
        ConsoleLogger.error("*******************************");
        ConsoleLogger.error("Could NOT connect to the server");
        ConsoleLogger.error("Entire test is aborted!!!");
        ConsoleLogger.error("Please check port and IP again!");
        ConsoleLogger.error("*******************************");
        throw e;
    }
}

Now, my test fails because I am getting an exception from the URL. I can see in the console the error message/s. This method is being called in a method with the annotation @BeforeSuite.

Why can’t I debug this method (not the try block nor the catch block)?

like image 797
Yishai Seela Avatar asked Apr 16 '19 10:04

Yishai Seela


2 Answers

You've mentioned that your breakpoints are not being respected by IntelliJ and I can see from your code that there are references to a server. This might be happening if you are trying to do remote debugging:

To debug your code on a server you have to attach your debugger to its debug port. This means starting the server in debug mode, and creating a Remote debugging run configuration in IntelliJ attached to the correct port (which should be reported in your server initialisation logging).

For more information read the the original answer here.

If you are not remote debugging you can try do to the following:

  • Clear your cache with File -> Invalidate Caches/Restart.
  • Delete your whole project folder and import it again.
  • Reinstall your IDE and delete all configurations.

I was assuming you are working in IntelliJ for some reason but all of these points should work for other programs as well.

like image 110
Matthew Avatar answered Oct 09 '22 05:10

Matthew


Since you can print out the error message, definitely you have chance to debug. You can try to set break point at that printing error message. If it still fail. Means your workspace is not update with your code. Just refresh or restart your IDE. Good luck.

like image 45
Qingfei Yuan Avatar answered Oct 09 '22 05:10

Qingfei Yuan