Over a million developers have joined DZone.

JUnit testing of Spring MVC application: Testing DAO layer

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

In continuation of my blog JUnit testing of Spring MVC application – Introduction, in this blog, I will show how to design and implement DAO layer for the Bookstore Spring MVC web application using Test Driven development.

For people in hurry, get the latest code from Github and run the below command

mvn clean test -Dtest=com.example.bookstore.repository.JpaBookRepositoryTest

As a part of TDD,

  • Write a basic CRUD (create, read, update, delete) operations on a Book DAO class com.example.bookstore.repository.JpaBookRepository. Don’t have the database wiring yet in this DAO class. Once we build the JUnit tests, we use JPA as a persistence layer. We also use H2 as a inmemory database for testing purpose.
  • Create Book POJO class

Create the JUnit test as below,

public class JpaBookRepositoryTest {
public void testFindById() {
Book book = bookRepository.findById(this.book.getId());
assertEquals(this.book.getAuthor(), book.getAuthor());
assertEquals(this.book.getDescription(), book.getDescription());
assertEquals(this.book.getIsbn(), book.getIsbn());

public void testFindByCategory() {
List<Book> books = bookRepository.findByCategory(category);
assertEquals(1, books.size());

for (Book book : books) {
assertEquals(this.book.getCategory().getId(), category.getId());
assertEquals(this.book.getAuthor(), book.getAuthor());
assertEquals(this.book.getDescription(), book.getDescription());
assertEquals(this.book.getIsbn(), book.getIsbn());

public void testStoreBook() {
Book book = new BookBuilder() {
title("John Doe's life");


Book book1 = bookRepository.findById(book.getId());

assertEquals(book1.getAuthor(), book.getAuthor());
assertEquals(book1.getDescription(), book.getDescription());
assertEquals(book1.getIsbn(), book.getIsbn());

If you notice since the JpaBookRepository is only a skeleton class without implementation, all the tests will fail.

As a next step, we need to create a Configuration and wire a datasource, and for the test purpose we will be using H2 database. And we also need to wire this back to JUnit test as below,

public class InfrastructureContextConfiguration {
private DataSource dataSource;
//some more configurations..
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.build();

//JUnit test wiring is as below

@ContextConfiguration(classes = { InfrastructureContextConfiguration.class, TestDataContextConfiguration.class })
public class JpaBookRepositoryTest {
//the test methods

Next step is to setup and teardown sample data in the JUnit test case as below,

public class JpaBookRepositoryTest {
private EntityManager entityManager;

private Book book;
private Category category;

public void setupData() {

category = new CategoryBuilder() {

book = new BookBuilder() {
description("Richard Dawkins' brilliant reformulation of the theory of natural selection");
author("Richard Dawkins");
title("The Selfish Gene: 30th Anniversary Edition");

public void tearDown() {

Once we do the wiring, we need to implement the com.example.bookstore.repository.JpaBookRepository and use JPA to do the CRUD on the database and run the tests. The tests will succeed.

Finally if you run Cobertura for this example from STS, we will get over 90% of line coverage for com.example.bookstore.repository.JpaBookRepository. In case you want to try few exercises you can implement repository for Account and User.

I hope this blog helped you. In my next blog I will talk about Mochito and Implementing the Service layer.

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.


Published at DZone with permission of Krishna Prasad, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}