/java/

Junit and Mockito, part 1

2013-01-23 00:23:31

  • @Mock
  • @InjectMocks
  • when...thenReturn
  • verify
  • ArgumentCaptor
  • Assert.assertEquals
  • assertTrue

PostServiceTest

package com.louis.core.service;

import static org.junit.Assert.*;
import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.*;

import com.louis.core.dao.PostDAO;
import com.louis.core.model.Post;

@RunWith(MockitoJUnitRunner.class)
public class PostServiceTest {

    @Mock
    public PostDAO postDAO;

    @InjectMocks
    public PostService postService;

    @Test
    public void testSave_1() {
        Post post = new Post();
        post.setName("Some Name");
        post.setDescription("Some Desc");
        post.setContent("content");
        post.setType(1);
        post.setStatus(true);

        when(postDAO.create(post)).thenReturn(10L);

        Long id = postService.save(post);
        assertEquals("R", id,  new Long(10));
    }

    @Test
    public void testSave_2() {
        Post post = new Post();
        postService.save(post);
        verify(postDAO).create(post);
    }

    @Test
    public void testSave_3() {
        Post post = new Post();
        ArgumentCaptor

<post> arg = ArgumentCaptor.forClass(Post.class);
        postService.save(post);

        verify(postDAO).create(arg.capture());

        Assert.assertEquals(post, arg.getValue());
    }

    @Test
    public void testGetPosts_methodReturnNewPost_returnTrue() {
        when(postDAO.getPost(10L)).thenReturn(new Post(10L,"New","Desc","Cont",true,1));

        Post retPost = postService.getOne( 10L );

        assertTrue( retPost.getId() != null );
        assertTrue( retPost.getName() != null );
        assertTrue( retPost.getDescription() != null );
        assertTrue( retPost.getContent() != null );
    }

}

PostDAO

package com.louis.core.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import com.louis.core.model.Post;

@Repository
public class PostDAO{

    @PersistenceContext
    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public Post getPost(Long id) {
        return getEntityManager().find(Post.class, id);
    }

    public Long create(Post post) {
        Post postInsert = getEntityManager().merge(post);
        return postInsert.getId();
    }

    public void update(Post post) {
        getEntityManager().persist(post);
    }

    public void delete(Post post) {
        getEntityManager().remove(post);
    }

    public List<post> getPosts() {
        TypedQuery<post> query = getEntityManager().createNamedQuery("Post.findAll", Post.class);
        return query.getResultList();
    }
}

PostService

package com.louis.core.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.louis.core.dao.PostDAO;
import com.louis.core.model.Post;

@Service
public class PostService {

    @Autowired
    private PostDAO postDAO;

    @Transactional
    public Long save(Post post) {
        return postDAO.create(post);
    }

    public List<post> getPosts() {
        return postDAO.getPosts();
    }

    public Post getOne(Long id){
        return postDAO.getPost(id);
    }

    @Transactional
    public void newStatus(Long id, boolean status){
        Post post = postDAO.getPost(id);
        post.setStatus(status);
        save(post);
    }

    @Transactional
    public void delete(Long id){
        Post post = postDAO.getPost(id);
        postDAO.delete(post);
    }
}

Maven:

<dependency>
<groupid>org.mockito</groupid>
<artifactid>mockito-core</artifactid>
<version>1.9.5</version>
</dependency>
<dependency>
<groupid>org.mockito</groupid>
<artifactid>mockito-all</artifactid>
<version>1.9.5</version>
</dependency>