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
2025-04-03, 301🔥, 0💬
Popular Posts:
How to validate domain name format? In order to help your programming or testing tasks, FYIcenter.co...
How to use HTTP protocol to post an XML message to a server? You can follow this tutorial to post an...
In what order thread groups are executed in JMeter? Thread groups, including regular "Thread Groups"...
Where to find tutorials on UFT (Unified Functional Testing) tool? I want to know how to use UFT. Her...
How to generate test phone numbers for US and Canada? Test phone numbers are frequently needed in te...