How to debug Unit Tests in WSO2 Code?

Tharika Madurapperuma
3 min readFeb 6, 2021

Some unit tests are failing after my changes! How can I debug the unit test I wrote to see if it is working fine? — Is this you? Then this article is for you.

Photo by Riku Lu on Unsplash

After writing a unit test, you almost always get the requirement to debug the test case that you wrote. Or else if you encounter a build failure due to a failing unit test, you need to always debug and find what went wrong.

In this article, I’m going to show you how.

In WSO2 API Manager for example, unit tests can be found in carbon-apimgt repository. So when you run the following command, all unit tests will also be executed.

mvn clean install

Method 1 : Debugging the test case from the IDE

Unit tests in WSO2 API Manager uses JUnit as the framework. Hence you can do the debugging easily from the IDE you use. Most probably this might be IntelliJ IDEA.

This is the method that is commonly used by many developers for JUnit based test cases. You can refer the following article to try that out.

https://www.jetbrains.com/help/idea/performing-tests.html

But this never works for me when debugging unit tests for carbon-apimgt repository. 😂 This might be the same for you too. So let me tell you the method I use.

Method 2 : Debugging by executing a mvn command

Assume you need to debug a test case in package org.wso2.carbon.apimgt.impl Then execute the following steps to start debugging the unit tests in that package.

  • Move into the relevant package root folder from the terminal. In our case this will be

<repository_root>/components/apimgt/org.wso2.carbon.apimgt.impl

  • Now execute the command below to start running the tests in debug mode.
mvn clean install -Dmaven.surefire.debug
  • You will now see the tests halted at the following line listening at port 5005.
Tests listening at port 5005
  • Now open your IDE and create a Debug Configuration. I am using IntelliJ IDEA here. Go to RunEdit Configurations. Click on the + button in the window and select Remote. Add a new configuration as follows and click ApplyOK. Make sure the port is 5005.
NewDebugConfig
  • Make sure to add breakpoints to the class that you want to debug in your IDE.
  • Click on the Debug icon in the toolbar after selecting the Debug Configuration you just created from the drop down menu.
Click on Debug
  • The tests will now start running and will hit the breakpoint you set up in your test class.
  • Now you can continue debugging the code as you like. 🙂

Thank you for reading the article!

If you find any outdated content or issues with this article, please feel free to create an issue at Developer Corner Git repository here. Let’s grow together and help others in their journey too!

If you like this article please give it a clap. 🙂

Happy Debugging..!

--

--