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, ∼359🔥, 0💬
Popular Posts:
How to valid UUID/GUID strings? In order to help your programming or testing tasks, FYIcenter.com ha...
How to generate user full names? Test user names are frequently needed in testing applications that ...
Where to find tutorials on Selenium test tools? I want to know how to use Selenium. Here is a large ...
How to generate user birthday dates? Test user birthday dates are frequently needed in testing appli...
How to convert a date and time to a standard format? Date and time can be displayed in a number of s...