First Word
Junit is open source testing framework developed for unit testing java code. It is now the default framework for testing Java development.
Is it popular?
A very interesting survey shows that JUnit is THE MOST POPULAR LIBRARY FOR JAVA used by 30.7% of projects (it tied with slf4j-api).
BTW, Apache Commons and Google Guava are the most important common libraries for Java.
Unit Test
A unit test is a piece of code that executes a specific functionality in the code. Tests should be written for critical and complex parts of your application (not every single statement). The percentage of code which is tested by unit tests is typically called test coverage.
Unit test is really a coding process, not a testing process.
In contrast, there is “Manual testing”.
Why should we use it?
Test early and often automated testing.
Junit tests can be integrated with the build so regression testing can be done at unit level.
Test code reusage.
Other kinds of tests?
Integration Test (also called Functional Test) test the behavior of a component or the integration between a set of components.
Performance tests are used to benchmark software components in a repeatable way
TDD
TDD (Test-Driven Development) write unit test before writing the code. It is commonly believed that tests should be written before the code during development cycle.
Some believes that TDD results in less time in the debugger, and more time thinking about outside-in design. Some also believes that TDD is more fun than writing tests after the code seems to be working.
Junit
In Java, ‘assert’ is a keyword. So, JUnit 3.7 deprecated assert() and replaced it with assertTrue(). JUnit 4 is compatible with the assert keyword.
Test Class/Suite
Test methods are contained in a class called Test class. JUnit assumes that all test methods can be executed in an arbitrary order (thus no dependency).
Several test classes can be combined into a test suite. Running a test suite will execute all test classes in the specified order.
Test fixtures
Fixtures is a fixed state of a set of objects used as a baseline for running tests. It includes:
setUp() method which runs before every test invocation.
tearDown() method which runs after every test method.
“extends TestCase” or @annotation?
JUnit 3-style: create a class that “extends TestCase”, and start test methods with the word test. In Eclipse, all methods named “testSomething” are automatically run.
JUnit 4-style: create a ‘normal’ class and prepend a @Test annotation to the method (name do not have to be “testSomething”). This is recommended way.
Other than normal test
There are:
Expected Exception Test: @Test(expected = ArithmeticException.class)
Ignore Test: @Ignore(“Not Ready to Run”)
Time Test: @Test(timeout = 1000)
Suite Test: @Suite.SuiteClasses({ JunitTest1.class, JunitTest2.class })
Parameterized Test: read below
Junit 4 has introduced a new feature Parameterized tests. Parameterized tests allow developer to run the same test many times using different values (passed in as a collecetion). One good example.
Miscellaneous
Q: What happens if a JUnit Test Method is Declared as “private”?
A: If a JUnit test method is declared as “private”, it compiles successfully. But the execution will fail. This is because JUnit requires that all test methods must be declared as “public”.
Q: How do you test a “protected” method?
Modifier | Class | Package | Subclass | World |
---|---|---|---|---|
public |
Y | Y | Y | Y |
protected |
Y | Y | Y | N |
no modifier | Y | Y | N | N |
private |
Y | N | N | N |
A: When a method is declared as “protected”, it can only be accessed within the same package where the class is defined. Hence to test a “protected” method, define test class in the same package.
Things that I don’t understand
Q: How do I test things that must be run in a J2EE container (e.g. servlets, EJBs)?
A: Refactoring J2EE components to delegate functionality to other objects that don’t have to be run in a J2EE container will improve the design and testability of the software. Cactus is an open source JUnit extension that can be used for unit testing server-side java code.
Q: What are Parameterized tests in JUnit?
A: Parameterized tests allow developer to run the same test over and over again using different values.