Categories:
Audio (13)
Biotech (29)
Bytecode (35)
Database (77)
Framework (7)
Game (7)
General (512)
Graphics (53)
I/O (32)
IDE (2)
JAR Tools (86)
JavaBeans (16)
JDBC (89)
JDK (337)
JSP (20)
Logging (103)
Mail (54)
Messaging (8)
Network (71)
PDF (94)
Report (7)
Scripting (83)
Security (32)
Server (119)
Servlet (17)
SOAP (24)
Testing (50)
Web (19)
XML (301)
Other Resources:
Sample Program for junit-4.8.1.jar
How to write a Sample program to use junit-4.8.1.jar?
✍: FYIcenter.com
JUnit is a simple framework to write repeatable tests for Java applications.
In order to use junit-4.8.1.jar,
we need to write a simple Java application, Calculator.java:
// Copyright (c) 2015 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 sample Java program is simple. But it does have some issues:
Let's write JUnit test program, CalculatorTest.jar, to show those issues:
// Copyright (c) 2015 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 sample 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 "1.8.0_45" \fyicenter>javac Calculator.java \fyicenter>javac -cp .;\local\lib\junit-4.8.1.jar CalculatorTest.java
Note that you need to provided junit-4.8.1.jar in the classpath to compile CalculatorTest.java
Back to FAQ for JUnit (Java Unit) Testing.
2016-02-29, 1873👍, 0💬
Popular Posts:
JDK 11 jdk.internal.le.jmod is the JMOD file for JDK 11 Internal Line Editing module. JDK 11 Interna...
maven-model-builder-3.5. 4.jaris the JAR file for Apache Maven 3.5.4 Model Builder module. Apache Ma...
Provides a simple high-level Http server API, which can be used to build embedded HTTP servers. Both...
iText is an ideal library for developers looking to enhance web- and other applications with dynamic...
JDK 11 jdk.jdeps.jmod is the JMOD file for JDK 11 JDeps tool, which can be invoked by the "jdeps" co...