TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

JUnit

JUnit : Calculator Test Case Example

JUnit Tutorial, Most Visited

Introduction

In this post, we will look at a Simple Calculator Test case example for writing JUnit Tests. This tutorial helps beginners to understand writing and executing JUnit Tests.

Manual Testcases for Standard Windows Calculator application can be found here:

https://www.testingdocs.com/manual-test-cases-for-calculator-application/

Calculator class

The Calculator class is the class to be tested. The class contains mathematical methods like add, subtract, multiply, divide methods that need to be tested. We will write JUnit test methods to test the methods.

Steps to create a Calculator Java class:  Create Calculator Java class

Calculator User Story

Sample User Story in Agile scrum story card format : 

https://www.testingdocs.com/sample-calculator-user-story/

 Calculator Flowchart

https://www.testingdocs.com/questions/calculator-raptor-flowchart/

JUnit Test mechanism

The @Test annotation methods test the Calculator object under test.

 

 

Adding @Test methods in Eclipse IDE

Steps to add test methods using Eclipse IDE:

https://www.testingdocs.com/adding-calculator-test-methods-in-eclipse/

 

AAA pattern

 

Calculator Test Case

The below example shows some basic sample JUnit test cases.

package com.testingdocs.calculator.tests;

//Arrange-Act-Assert pattern
import com.testingdocs.calculator.Calculator;
import org.junit.Test;
import org.junit.Assert;
import org.junit.Before;

public class CalculatorTest {
private Calculator objCalcUnderTest;

@Before
public void setUp() {
//Arrange
objCalcUnderTest = new Calculator();
}

@Test
public void testAdd() {
int a = 15; int b = 20;
int expectedResult = 35;
//Act
long result = objCalcUnderTest.add(a, b);
//Assert
Assert.assertEquals(expectedResult, result);
}

@Test
public void testSubtract() {
int a = 25; int b = 20;
int expectedResult = 5;
long result = objCalcUnderTest.subtract(a, b);
Assert.assertEquals(expectedResult, result);
}

@Test
public void testMultiply() {
int a = 10; int b = 25;
long expectedResult = 250;
long result = objCalcUnderTest.multiply(a, b);
Assert.assertEquals(expectedResult, result);
}

@Test
public void testDivide() {
int a = 56; int b = 10;
double expectedResult = 5.6;
double result = objCalcUnderTest.divide(a, b);
Assert.assertEquals(expectedResult, result,0.00005);
}

@Test(expected = IllegalArgumentException.class)
public void testDivideByZero() {
int a = 15; int b = 0;
objCalcUnderTest.divide(a, b);
}
}

 

 

 

Screenshot

 

Calculator Test case 

Right-click the JUnit test example class and run the test methods as “JUnit Test”. 

In this example, we have placed Calculator class( the test class intended to be tested) in the same package as the test-case example class. However, this might not be the case while writing real tests. The application source code can be separated from test source code or automation code.

Debug JUnit Tests

When tests fail without valid defects in the application code, we may want to debug the tests. Learn how to debug a failing JUnit test in Eclipse.

https://www.testingdocs.com/debugging-junit-tests-in-ide/

Jenkins

Running Calculator tests using Jenkins job. In real projects, the development team wants to run the tests automatically for every code change to the source code. Jenkins is a Continuous Integration (CI ) tool that does the job automatically. 

https://www.testingdocs.com/running-calculator-junit-tests-using-jenkins/

Using Command Line

To run the tests from the command line: tests from command line.

Bank Account Example

Testing Bank Account Java class with JUnit 5:

Sample Savings Account Junit Tests

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

Test Failure JUnit

JUnit /

Debug JUnit Tests using IDE

‹ Assumptions in JUnit Tests› Calculator Java class

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version