JUnit testing of Spring MVC application: Testing Service layer

[I have added a new Spring Boot Tutorial on this, please review give me feedback.]

In continuation of my earlier blogs on Introduction to Spring MVC and Testing DAO layer in Spring MVC, in this blog I will demonstrate how to test Service layer in Spring MVC. The objective of this demo is 2 fold, to build the Service layer using TDD and increase the code coverage during JUnit testing of Service layer.

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

mvn clean test -Dtest=com.example.bookstore.service.AccountServiceTest

Since in my earlier blog, we have already tested the DAO layer, in this blog we only need to focus on testing service layer. We need to mock the DAO layer so that we can control the behavior in Service layer and cover various scenarios. Mockito is a good framework which is used to mock a method and return known data and assert that in the JUnit.

As a first step we define the AccountServiceTestContextConfiguration class with AccountServiceTest class. If you notice there are 2 beans defined in that class and we marked the as a @Configuration which shows that it is a Spring Context class. In the JUnit test we @Autowired AccountService class. And AccountServiceImpl @Autowired the AccountRepository class. When creating the Bean in the configuration file we also stubbed the AccountRepository class using Mockito,

public class AccountServiceTest {

static class AccountServiceTestContextConfiguration {
public AccountService accountService() {
return new AccountServiceImpl();
public AccountRepository accountRepository() {
return Mockito.mock(AccountRepository.class);

//We Autowired the AccountService bean so that it is injected from the configuration
private AccountService accountService;
private AccountRepository accountRepository;

During the setup of the JUnit we use Mockito mock findByUsername method to return a predefined account object as below

public void setup() {
Account account = new AccountBuilder() {
address("Herve", "4650", "Rue de la gare", "1", null, "Belgium");
credentials("john", "secret");
name("John", "Doe");

Now we write the tests as below and test both the positive and negative scenarios,

@Test(expected = AuthenticationException.class)
public void testLoginFailure() throws AuthenticationException {
accountService.login("john", "fail");
public void testLoginSuccess() throws AuthenticationException {
Account account = accountService.login("john", "secret");
assertEquals("John", account.getFirstName());
assertEquals("Doe", account.getLastName());

Finally we verify if the findByUsername method is called only once successfully as below in the teardown,

public void verify() {
Mockito.verify(accountRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());
// This is allowed here: using container injected mocks

AccountService class looks as below,

@Transactional(readOnly = true)
public class AccountServiceImpl implements AccountService {

private AccountRepository accountRepository;

public Account login(String username, String password) throws AuthenticationException {
Account account = this.accountRepository.findByUsername(username, password);
} else {
throw new AuthenticationException("Wrong username/password", "invalid.username");
return account;

I hope this blog helped you. In my next blog, I will demo how to build a controller JUnit test.


Pro Spring MVC: With Web Flow by by Marten Deinum, Koen Serneels

2 thoughts on “JUnit testing of Spring MVC application: Testing Service layer

  1. Pingback: JUnit testing of Spring MVC application – Introduction | Krishna's Blog

  2. Pingback: JUnit testing of Spring MVC application: Testing Controller | Krishna's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s