lundi 27 novembre 2017

Testing service layer with Mockito/Spring with constructor injection

I use constructor injection so my test should look like this now.

import org.junit.Assert;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.user.model.jpa.User;
import com.user.repository.UserRepository;
import com.user.service.UserService;
import com.job.service.JobService;

@RunWith(MockitoJUnitRunner.class)
public class UserServiceImplTest {


    @Mock
    private UserRepository userRepository;

    @Mock
    private JobService jobService;

    @Mock
    private User testObject;

    private UserService userService;

    @Before
    public void setUp() {
        userService = new UserServiceImpl(userRepository, jobService);
    }
    @Test
    public void testAdd() {

        //given
        testObject = new Mock(User.class);  //commented line
        testObject.setName("name");

        //when
        userService.add(user);

        //then
        Assert.assertEquals("name",task.getName());
    }
}

Is this possible to mock like I did in commented line?

I want to avoid creating new object manually with all fields, because it has too much fields and contaings of two other classes (id, name, surname, Address address, Job job etc.)

Aucun commentaire:

Enregistrer un commentaire