TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • TestNG
  • Tools
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

JUnit

Debug JUnit Tests using IDE

Introduction

In this tutorial, we will learn how to debug Calculator JUnit Tests using IDE like Eclipse. We would consider the Calculator JUnit Project and the tests that test the Calculator Java class. Follow the links for the basic setup of the JUnit Calculator Test project in Eclipse IDE.

Creating a Maven Project

https://www.testingdocs.com/create-quickstart-maven-project-in-eclipse-ide/

Calculator Example

https://www.testingdocs.com/junit-calculator-test-case-example/

Failing Test

Let’s make a test fail and try to debug the failing test in order to fix the test method. We will seed a fault in the test, and assume that the test is false positive and there is no bug in the application. Sometimes, it might not be true and the failing test might be a valid application bug, in that case you may need to log the issue in the Bug tracker application.

 

public void testAdd() {
 int a = 15;
 int b = 21;
 int expectedResult = 35;
 long result = objCalcUnderTest.add(a, b);
 Assert.assertEquals(expectedResult, result);
 }

This test would fail with the trace:

java.lang.AssertionError: expected:<35> but was:<36>
at org.junit.Assert.fail(Assert.java:91)

 

Test Failure JUnit

Steps to Debug

Launch Eclipse IDE.
Open the Calculator Project.
Open the Junit Test case which has the @Test annotated methods.

Set Breakpoint

Set Breakpoint on the statements that throw error/ exceptions etc.

To set a breakpoint, Right-click and choose Toggle breakpoint.

 

Toggle Breakpoint

To Debug the JUnit Test, use the below option:

Right Click >> Debug As >> Junit Test.

 

RunAs Debug Junit Test

Analyze

Analyze the variables, values, calculations, reference variables, etc in the debug window. Find out the root causes of errors, exceptions, etc.

Fix the tests based on your observations.

We can step into, step over multiple breakpoints during the debug session.

Variables in Debug mode

Fix

Based on the analysis, fix the test. In this case, though we have seeded a fault in the test, the expected Result is wrong. We have mistaken that

15 + 21  as 35 expected result which is wrong. Make the expected result 36 to fix the test. We can debug the tests for exceptions, erroneous expectations, variable watch etc.

 

JUnit Tutorial on this website :

https://www.testingdocs.com/junit-tutorial/

More information on JUnit can be found on the official website:

https://junit.org

Related Posts

Getting Started with JUnit Framework

JUnit /

Getting Started with JUnit Framework

Run JUnit tests from command line

JUnit /

Run JUnit tests from command line

Working with JUnit in Eclipse IDE

JUnit /

Working with JUnit in Eclipse IDE

Adding JUnit5 library to a Project

JUnit /

Adding JUnit5 library to a Project

Adding Calculator Test Methods in Eclipse

JUnit /

Adding Calculator Test Methods in Eclipse

‹ Adding Calculator Test Methods in Eclipse› Adding JUnit5 library to a Project

Recent Posts

  • Antivirus Products for Windows 11 PC
  • Install CodeBlocks IDE on Windows 11
  • Location Privacy Settings on Windows
  • Windows 11 Virus & Threat Protection
  • Download & Install Python on Windows 11
  • Cumulative Update for Windows 11 Version
  • Test execution speed in Selenium IDE
  • Check Windows 11 Free Upgrade
  • Download IntelliJ IDE on Windows 11
  • New MySQL Connection in Workbench

Back to Top

Automation Tutorials

  • JBehave Tutorial
  • Selenium Tutorial
  • TestNG Tutorial
  • JUnit Tutorial

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version