如何写一个junit mokito测试用例

时间:2017-08-18 07:25:07

标签: java spring-mvc junit mockito

如何使用if-else条件示例编写Junit,Mockito测试用例来绑定结果@Valid功能?

@RequestMapping(value = "/scriptfile", method = RequestMethod.POST)     
public String create(
    @Valid ScriptFile scriptFile, 
    BindingResult result, 
    ModelMap modelMap
) 
{         
    if (scriptFile == null) 
        throw new IllegalArgumentException("A scriptFile is required"); 

    if (result.hasErrors()) {                 
        modelMap.addAttribute("scriptFile", scriptFile);                     
        modelMap.addAttribute("showcases", ShowCase.findAllShowCases());                     
        return "scriptfile/create";                 
    }             
    scriptFile.persist();             
    return "redirect:/scriptfile/" + scriptFile.getId();         
}   

如何为上述示例编写Junitm Mockito测试用例?

4 个答案:

答案 0 :(得分:1)

这将是这样的。如果可以实例化对象,则可以使用spy而不是mock。或者甚至创建一个ModelMap实例并检查属性是否设置正确而不是模拟。

@Mock
private BindingResult result;

@Mock
private ModelMap modelMap;

@Mock 
private ScriptFile scriptFile;

@BeforeMethod
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
}

@Test(expectedExceptions = {IllegalArgumentException.class})  
public void shouldThrowIllegalArgumentException() {
    create(null, result, modelMap);
}

@Test
public void shouldPersistFile() {
    Mockito.when(result.hasErrors()).thenReturn(false);
    Mockito.when(scriptFile.getId()).thenReturn("FILE_ID");

    String output = create(scriptFile, result, modelMap);

    Mockito.verify(scriptFile, times(1)).persist();
    assertEquals("redirect:/scriptfile/FILE_ID", output)
}

@Test
public void shouldHandleErrors() {
    Mockito.when(result.hasErrors()).thenReturn(true);
    Mockito.when(scriptFile.getId()).thenReturn("FILE_ID");

    String output = create(scriptFile, result, modelMap);

    Mockito.verify(modelMap, times(2)).addAttribute(Mockito.any(), Mockito.any());
    assertEquals("scriptfile/create", output)
}

答案 1 :(得分:0)

我会使用mockito来模拟Binding结果和ScriptFile类:

@Test
public void  create_WithErrors(){
    BindingResult bindingResult = Mockito.mock(BindingResult.class);
    BindingResult scriptFile = Mockito.mock(ScriptFile.class);
    Mockito.when(bindingResult.hasErrors()).thenReturn(true);
    String result = create(scriptFile, bindingResult, new ModelMap());
    Mockito.verify(bindingResult.hasErrors());
    Mockito.verify(scriptFile .persist(), Mockito.never());
    assertThat(result, is(equalTo("scriptfile/create")))
}

答案 2 :(得分:0)

使用spring-test MockMvC,有很多现有代码,其中之一是:

Using Spring MVC Test to unit test multipart POST request

你的代码片段不起作用,你没有绑定params(@RequestParam) 而不是@RequestMapping使用@PostMapping

答案 3 :(得分:0)

这里是模拟测试的一个例子。

package com.ase.PRODUCTS.mockito.service;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ase.PRODUCTS.bean.AuthenticationStatus;
import com.ase.PRODUCTS.bean.SignUpStatus;
import com.ase.PRODUCTS.bean.User;
import com.ase.PRODUCTS.dao.UserDao;
import com.ase.PRODUCTS.repository.UserRepository;
import com.ase.PRODUCTS.service.UserService;

public class UserServiceTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceTest.class);

    @Mock
    UserRepository mockedUserRepository;
    // UserDao mockedUserDao;

    @InjectMocks
    UserService service;

    @Before
    public void setup() {

        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void signUpNewUser() {
        LOGGER.info("START : Inside Unit Testing addUser() method of UserServiceTest");
        User user = new User();
        user.setEmail("kiran@gmail.com");
        LOGGER.debug("User Object :  {}", user);
        when(mockedUserRepository.findUserByEmail(user.getEmail())).thenReturn(null);

        SignUpStatus expectedStatus = new SignUpStatus(true, false);
        SignUpStatus actualStatus = service.addUser(user);

        LOGGER.debug("Expected  Output :  {}", expectedStatus);
        LOGGER.debug("Actual  Output :  {}", actualStatus);

        Assert.assertEquals(true, expectedStatus.equals(actualStatus));
        verify(mockedUserRepository, times(1)).save(Mockito.any(User.class));
        verify(mockedUserRepository, times(1)).findUserByEmail(user.getEmail());

        LOGGER.info("End : Unit Testing addUser() method of UserServiceTest");
    }

    @Test
    public void emailAlreadyExists() {
        LOGGER.info("START : Inside Unit Testing addUser() method of UserServiceTest");
        User user = new User();
        user.setEmail("kiran@gmail.com");
        LOGGER.debug("User Object :  {}", user);
        when(mockedUserRepository.findUserByEmail(user.getEmail())).thenReturn(user);

        SignUpStatus expectedStatus = new SignUpStatus(false, true);
        SignUpStatus actualStatus = service.addUser(user);

        LOGGER.debug("Expected  Output :  {}", expectedStatus);
        LOGGER.debug("Actual  Output :  {}", actualStatus);

        Assert.assertEquals(true, expectedStatus.equals(actualStatus));
        verify(mockedUserRepository, times(0)).save(Mockito.any(User.class));
        verify(mockedUserRepository, times(1)).findUserByEmail(user.getEmail());

        LOGGER.info("End : Unit Testing addUser() method of UserServiceTest");
    }

    @Test
    public void loginValidUser() {
        LOGGER.info("START : Inside Unit Testing addUser() method of UserServiceTest");
        User user = new User();
        user.setEmail("kiran@gmail.com");
        user.setPassword("123456");
        LOGGER.debug("User Object :  {}", user);
        when(mockedUserRepository.findUserByEmail(user.getEmail())).thenReturn(user);

        AuthenticationStatus expectedStatus = new AuthenticationStatus();
        expectedStatus.setAuthenticated(true);
        expectedStatus.setActualUser(user);
        AuthenticationStatus actualStatus = service.loginUser(user);

        LOGGER.debug("Expected  Output :  {}", expectedStatus);
        LOGGER.debug("Actual  Output :  {}", actualStatus);

        Assert.assertEquals(true, expectedStatus.equals(actualStatus));
        verify(mockedUserRepository, times(0)).save(Mockito.any(User.class));
        verify(mockedUserRepository, times(1)).findUserByEmail(user.getEmail());

        LOGGER.info("End : Unit Testing addUser() method of UserServiceTest");
    }

    @Test
    public void testLoginForInvalidEmail() {
        LOGGER.info("START : Inside Unit Testing addUser() method of UserServiceTest");
        User user = new User();
        user.setEmail("kiran@gmail.com");
        user.setPassword("123456");
        LOGGER.debug("User Object :  {}", user);

        AuthenticationStatus expectedStatus = new AuthenticationStatus();
        expectedStatus.setAuthenticated(false);
        expectedStatus.setActualUser(null);

        User actualUser = new User();
        actualUser.setEmail("kiran.com");
        actualUser.setPassword("123456");

        when(mockedUserRepository.findUserByEmail(user.getEmail())).thenReturn(actualUser);

        AuthenticationStatus actualStatus = service.loginUser(user);

        LOGGER.debug("Expected  Output :  {}", expectedStatus);
        LOGGER.debug("Actual  Output :  {}", actualStatus);

        Assert.assertEquals(true, expectedStatus.equals(actualStatus));

        verify(mockedUserRepository, times(1)).findUserByEmail(user.getEmail());

        LOGGER.info("End : Unit Testing addUser() method of UserServiceTest");
    }

    @Test
    public void testLoginForInvalidPassword() {
        LOGGER.info("START : Inside Unit Testing addUser() method of UserServiceTest");
        User user = new User();
        user.setEmail("kiran@gmail.com");
        user.setPassword("123456");
        LOGGER.debug("User Object :  {}", user);

        AuthenticationStatus expectedStatus = new AuthenticationStatus();
        expectedStatus.setAuthenticated(false);
        expectedStatus.setActualUser(null);

        User actualUser = new User();
        actualUser.setEmail("kiran@gmail.com");
        actualUser.setPassword("1256");

        when(mockedUserRepository.findUserByEmail(user.getEmail())).thenReturn(actualUser);

        AuthenticationStatus actualStatus = service.loginUser(user);

        LOGGER.debug("Expected  Output :  {}", expectedStatus);
        LOGGER.debug("Actual  Output :  {}", actualStatus);

        Assert.assertEquals(true, expectedStatus.equals(actualStatus));

        verify(mockedUserRepository, times(1)).findUserByEmail(user.getEmail());

        LOGGER.info("End : Unit Testing addUser() method of UserServiceTest");
    }

}