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.
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.
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.
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
- 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.
- Now open your IDE and create a Debug Configuration. I am using IntelliJ IDEA here. Go to Run → Edit Configurations. Click on the + button in the window and select Remote. Add a new configuration as follows and click Apply → OK. Make sure the port is 5005.
- 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.
- 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. 🙂