Collections:
Simple Test Program for junit-4.12.*.jar
How to write a Sample program to use junit-4.12.*.jar?
✍: Guest
In order to test junit-4.12.*jar,
we need to write a simple Java application, Calculator.java:
// Copyright (c) FYIcenter.com
public class Calculator {
public int evaluate(String expression) {
int sum = 0;
for (String summand: expression.split("\\+"))
sum += Float.valueOf(summand);
return sum;
}
}
This Java application is simple. But it does have some issues:
Let's write JUnit test program, CalculatorTest.java, to show those issues:
// Copyright (c) FYIcenter.com
import junit.framework.TestCase;
public class CalculatorTest extends TestCase {
public void testAddition() {
Calculator calculator = new Calculator();
int sum = calculator.evaluate("1+2+3");
assertEquals(6, sum);
}
public void testFraction() {
Calculator calculator = new Calculator();
int sum = calculator.evaluate("2.4+2.6");
assertEquals(5, sum);
}
public void testSubtraction() {
Calculator calculator = new Calculator();
int sum = calculator.evaluate("10-1");
assertEquals(9, sum);
}
}
This test program, CalculatorTest.java, does the following:
Of course, you need to compile them with JDK to make them ready to run:
fyicenter> java -version java version "21.0.2" 2024-01-16 LTS fyicenter> javac Calculator.java fyicenter> javac -cp .;junit-4.13.2.jar CalculatorTest.java fyicenter> javac -cp .:junit-4.13.2.jar CalculatorTest.java
Note that you need to provided junit-4.12.jar in the classpath to compile CalculatorTest.java
⇒ Run Sample Program with junit-4.*.jar
2026-01-07, ∼556🔥, 0💬
Popular Posts:
Where to find tutorials on UFT (Unified Functional Testing) tool? I want to know how to use UFT. Her...
How to generate user full names? Test user names are frequently needed in testing applications that ...
How to convert a date and time to a standard format? Date and time can be displayed in a number of s...
How to validate and decode MAC (Media Access Control) addresses? In order to help your programming o...
How to generate email addresses? To help you to obtain some email addresses for testing purpose, FYI...