Java Software Testing tutorials: unit testing, open source, JUnit, Mockito, TestNG, Spring, JGiven, etc.
The open source JUnit unit testing tool has been a blessing for Java developers. However, many programmers think that it is enough to learn the JUnit API and write a few tests in order to have a well-tested application. This idea is more dangerous than not doing unit tests because it leads to a wrong sense of code quality. Learning JUnit is the easiest part of unit testing your Java code, but writing good tests is the hard part.
Issues with testability in Java boil down to our inability to write tests or the excess trouble we have to go through to get it done. In this article, based on chapter 7 of the book “Effective Unit Testing – A guide for Java developers”, Lasse Koskela shares a set of dos and don’ts for testable design. In the tips provided, he recommends to avoid complex private methods, static methods, logic in constructors and to favor composition over inheritance.
The testing strategy to adopt when you you run your code inside a Java EE container is the topic discussed in this blog post by Antonio Goncalves. To solve this issue, he presents a detailed step by step process to unit test an EJB with Mockito and how to do integration test with and without Arquillian with code samples. His conclusion is that since Java EE 6 it is now easy to use container and services in an embedded mode. Unit testing is good to test business code or code in isolation (mocking external components) but you have to remember that you should also use integration testing to test code interacting with external components or services.
In the context of Java unit testing, a sleeping snail is a test that’s sluggish and takes (relatively speaking) forever to run because it relies on Thread#sleep and arbitrarily long waits to allow threads to execute before performing assertions or continuing with the workflow under test. In this article, based on chapter 5 of Unit Testing in Java, author Lasse Koskela explains this code smell and the appropriate deodorant with an example
Mockito is an open source mocking framework for Java. In this series of blog posts, Holger Staudacher shares his experiences with Mockito. He defines “effective” as arriving at clean test and production code as fast as possible. The first post of Effective Mockito explains how to setup Mockito in the Eclipse IDE for the daily work. The second post focuses on Mockito’s @Mock Annotation.
Thucydides is an open source tool that lets you use WebDriver-based unit or Behavior Driven Development (BDD) tests to write more flexible and more reusable WebDriver-based tests, and also to generate documentation about your acceptance tests. In this blog post, John Ferguson Smart explains how you can use Spring dependencies in your acceptance tests with Thucydides if you need to run your acceptance tests against an embedded web server.
Integration tests are performed after a successful execution of unit tests. Integration tests are, therefore, executed less frequently, because unit tests will naturally fail often. With this strict separation between unit tests and integration tests, we can save several minutes (sometimes, hours) with each turnaround. This article explains how integration testing can increase your productivity and ensure the deployability of your Java EE 6 application.