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, ∼477🔥, 0💬
Popular Posts:
How to validate Mod 10 (Luhn Algorithm) checksum? In order to help your programming or testing tasks...
How to generate email addresses? To help you to obtain some email addresses for testing purpose, FYI...
How to generate date and time test values? Timestamp values are frequently needed in testing almost ...
How to generate passwords? To help you to obtain some good passwords for testing purpose, FYIcenter....
How to generate MAC addresses? To help you to obtain some MAC addresses for testing purpose, FYIcent...