JUnit General Info
(Free Web Tutorials)
by Michael Thomas
JUnit Home Page
This tutorial describes what JUnit is.
JUnit General Info
- Description
- JUnit is used to test your Java classes.
- Wikipedia says:
JUnit is a unit testing framework for the Java programming language. JUnit
has been important in the development of test-driven development, and is one
of a family of unit testing frameworks collectively known as xUnit that
originated with SUnit.
JUnit is linked as a JAR at compile-time; the framework resides under
packages junit.framework for JUnit 3.8 and earlier and under org.junit for
JUnit 4 and later.
- JUnit is a very popular test frame work used in TDD (Test Driven Development).
In TDD all of your requirements are captured in the JUnit test class.
You write tests before you write code. Therefore you create a JUnit test class
before you create your class. Then in short iterative cycles you
create test methods in your JUnit to test a requirement before you write the
code that implements the requirement. You write a test method and see
it fail (red bar). Then you fake it to make it pass (green bar). Then
you make it right and see it still pass (green bar)
- JUnit is an automated unit testing framework for Java
- With JUnit Test code:
- test the published API for the class and all requirements.
- test for positive and negative scenarios.
- write a tests for bugs that are found.
- test getters/setters only if there is special logic involved.
- becomes apart of the codebase (but may not be deployed to production).
- should be documented.
- Assert Test Methods
- assertEquals(expected, actual) - uses the equals() method for objects.
Primitives uses the value as expected.
also: assertEquals(description, expected, actual).
- assertEquals(double expected, double actual, double delta) - expected
values are considered equal if within the delta value.
ex:
- assertSame(expected, actual) - for objects only.
- assertTrue(actual) - fails if actual is false.
- assertFalse(actual) - fails if actual is true.
- assertNull(actual) - fails if actual is not null.
- assertNotNull(actual) - fails if actual is not null.