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, ∼565🔥, 0💬
Popular Posts:
How to generate test phone numbers for US and Canada? Test phone numbers are frequently needed in te...
Where to find tutorials on Apache JMeter test tool? I want to know how to use Apache JMeter. Here is...
How to see my IP address Host Name? To help you to see your IP Address Host Name, FYIcenter.com has ...
How to validate Mod 10 (Luhn Algorithm) checksum? In order to help your programming or testing tasks...
How to find out my browser request headers? To help you to see your browser request headers, FYIcent...