Ajinkya
Ajinkya

Reputation: 22720

Junit to test Spring service and DAO layer

I am working on a Spring application (Spring 3.0) and following layered architecture i.e. Controller -> Service -> DAO layers.

I want to write unit test cases for service and DAO layer using Junit.
I checked Spring official site and also tried many other sites but couldn't figure out an easy and simple way of doing it.

Can anybody provide me some helpful resources ?


EDIT :
Looks like Mockito is the good option. Any good link to use it in Spring.

Thank you Alex for suggesting it.

Upvotes: 25

Views: 48926

Answers (3)

Vijay
Vijay

Reputation: 391

A DAO unit test (specifically a unit test not an integration test) which uses a Repo can be done like the following code snippet. No need to use things like the following as they are spring integration test-related annotations

@TransactionConfiguration(defaultRollback = true)
@ContextConfiguration({ "classpath:test-spring-context.xml" })

but if you want to test using test db and retrieving from DB you need to use @SpringBootTest and other integration test annotation

public class CrudDaoTest {
    @InjectMocks
    CrudDao crudDao = new CrudDaoImpl();

    @Mock
    DataRepo dataRepo;

    @BeforeEach
    public void init() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    public void getNamesTest(){
        when(dataRepo.findAll()).thenReturn(new ArrayList<>());
        List<DatEntity> res = crudDao.getNames();
        Assertions.assertNotNull(res);
    }
}

Upvotes: 0

Pete
Pete

Reputation: 10928

Don't know much about resources, but it's not that hard to do if you have your dao + spring setup nicely. You'll need the following:

  1. JUNIT dependencies obivously. with maven, something like that:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
    </dependency>
    
  2. The test class, which you place inside src/test/java:

    @TransactionConfiguration(defaultRollback = true)
    @ContextConfiguration({ "classpath:test-spring-context.xml" })
    @Transactional  
    @RunWith(SpringJUnit4ClassRunner.class)  
    public class SomeTests { 
        // ...
    }
    
  3. The context file for your spring setup referencing your DAO datasource placed inside src/test/resources. Somewhere in your test-spring-context.xml:

    <import resource="datasource-test.xml" />
    

Now for example in eclipse you can run the project as a JUNIT test.

Need more details? Is this solution applicable?

Upvotes: 4

Alex Barnes
Alex Barnes

Reputation: 7218

In terms of resources the Spring documentation on testing is very good. This can be found here.

When you test your service layer you will want to use a mocking library such as Mockito to mock your DAOs and therefore your domain layer. This ensures that they are true unit tests.

Then to integration test your DAOs against a database you can use the Spring transactional test utilities described in that reference documentation.

Upvotes: 26

Related Questions