Spring / Mockito Junit测试Null指针异常

时间:2018-09-02 19:42:12

标签: spring junit mockito

试图使带有模拟测试的JUnit正常工作,并在此处进行NPE测试。我想验证一下,当调用createRegistrant(registrationDTO)时,之后立即调用saveUpdateRegistration(Registration reg)方法。我发现一个奇怪的问题是,在Mockito.verify(registrationServiceImpl, Mockito.times(1)).createRegistrant(registrationDTO);上,这并不表示我可以测试是否调用了saveUpdateRegistration(Registration reg)方法吗? IntelliJ只建议我界面中的方法吗?

我的测试代码如下:

@RunWith(MockitoJUnitRunner.class)
public class RegistrationServiceImplTest {

    @InjectMocks
    private RegistrationServiceImpl registrationServiceImpl;

    @Mock
    private RegistrationDTO registrationDTO;

    @Mock
    private Registration registration;


    @Before
    public void setup() {
        registrationDTO.seteId("abc123");
        registrationDTO.setTech("AWS");
        registrationDTO.setFirstName("Bob");
        registrationDTO.setLastName("Tom");

    }


    @Test
    public void createRegistrant() throws Exception {
//        Mockito.when(registrationServiceImpl.createRegistrant(registrationDTO)).thenReturn(registrationDTO);
        Mockito.when(registrationServiceImpl.createRegistrant(registrationDTO)).thenReturn(registrationDTO);
        registrationServiceImpl.createRegistrant(registrationDTO);
        Mockito.verify(registrationServiceImpl, Mockito.times(1)).createRegistrant(registrationDTO);
    }

这是我正在测试的代码:

package com.TechODex.service;

import com.TechODex.dto.RegistrationDTO;
import com.TechODex.model.Registration;
import com.TechODex.dao.RegistrationDAO;

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

import java.util.List;

@Service
@Transactional
public class RegistrationServiceImpl implements RegistrationService {

    @Autowired
    private RegistrationDAO registrationDAO;

    public RegistrationDTO createRegistrant(RegistrationDTO reg) {
        saveUpdateRegistration(setDTOToModel(reg));
        return reg;
    }
    public void deleteRegistrant(Long id) { deleteUserRegistrant(id); }
    public List<Registration> findAllRegistrant(String tech) { return findAllRegistrantDAO(tech);}
    public List<Registration> findAllTech() { return findAllTechDAO();}


    private Registration setDTOToModel(RegistrationDTO dto){
        Registration registration = new Registration();

        registration.setFirstName(dto.getFirstName());
        registration.setLastName(dto.getLastName());
        registration.seteId(dto.geteId());
        registration.setTech(dto.getTech());

        return registration;
    }

    private void saveUpdateRegistration(Registration reg){ registrationDAO.save(reg);}

}

1 个答案:

答案 0 :(得分:0)

这里有几个问题。

首先,您不模拟RegistrationDAO。这将导致它成为被测类中的null。接下来,RegistrationService不是一个模拟,但是您尝试在其上使用whenverify。最后,RegistrationDTO被模拟,因此@Before中对setter的调用无效。

您的测试应类似于:

@RunWith(MockitoJUnitRunner.class)
public class RegistrationServiceImplTest {

    @InjectMocks
    private RegistrationServiceImpl registrationServiceImpl;

    @Mock
    private RegistrationDAO registrationDAO;

    private RegistrationDTO registrationDTO;
    private Registration registration;

    @Before
    public void setup() {
        registration = new Registration();
        registration.seteId("abc123");
        registration.setTech("AWS");
        registration.setFirstName("Bob");
        registration.setLastName("Tom");

        registrationDTO = new RegistrationDTO();
        registrationDTO.seteId("abc123");
        registrationDTO.setTech("AWS");
        registrationDTO.setFirstName("Bob");
        registrationDTO.setLastName("Tom");
    }

    @Test
    public void createRegistrant() throws Exception {
        final RegistrationDTO result = registrationServiceImpl.createRegistrant(registrationDTO);
        assertSame(registrationDTO, result);        

        verify(registrationDAO).save(eq(registration));
    }
}

假设Registration具有已实现的equals-method,该方法将比较设置的字段。如果没有,则需要使用ArgumentCaptor