Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
Sample Program for junit-4.12.jar
How to write a Sample program to use junit-4.12.jar?
✍: FYIcenter.com
JUnit is a simple framework to write repeatable tests for Java applications. In order to use junit-4.12.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.12.jar CalculatorTest.java
Note that you need to provided junit-4.12.jar in the classpath to compile CalculatorTest.java
⇒ Run org.junit.runner.JUnitCore in junit-4.12.jar
2016-02-24, 2528🔥, 0💬
Popular Posts:
JDK 11 jdk.jconsole.jmod is the JMOD file for JDK 11 JConsole tool, which can be invoked by the "jco...
JRE 8 rt.jar is the JAR file for JRE 8 RT (Runtime) libraries. JRE (Java Runtime) 8 is the runtime e...
JDK 11 java.security.jgss.jmod is the JMOD file for JDK 11 Security JGSS (Java Generic Security Serv...
JDK 17 jdk.jdi.jmod is the JMOD file for JDK 17 JDI (Java Debug Interface) tool. JDK 17 JDI tool com...
How to read XML document with XML Schema validation from socket connections with the socket\DelayedI...