spring mock数据单元测试

依赖的pom


            org.evosuite
            evosuite-standalone-runtime
            1.0.6
            test
        
        
            junit
            junit
            4.12
            test
        
        
            org.powermock
            powermock-api-mockito
            1.7.3
            test
        
        
            org.powermock
            powermock-module-junit4
            1.7.3
            test
        
        
            org.javassist
            javassist
            3.18.2-GA
        

代码

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;

/**
 * @Description:
 * @Author: 
 * @CreateDate: 2019/12/13 16:24
 * @Version: 1.0
 */
@PowerMockIgnore("*")
@RunWith(PowerMockRunner.class)
public class UserRegisterActivityEventProcessorTest {

    @InjectMocks
    private UserRegisterActivityEventProcessor userRegisterActivityEventProcessor;

    @Mock
    private ProductCustomerRelService productCustomerRelService;


    @Test
    public void  userRegister(){
        PowerMockito.when(productCustomerRelService.addRecord(null)).thenReturn(1);
        EventDTO eventDTO =new EventDTO();
        eventDTO.setEventType(EventEnum.USER_REGISTER.name());
        eventDTO.setData("{\"memberId\":\"1\",\"referrerMemberId\":\"2\"}");
        userRegisterActivityEventProcessor.handle(eventDTO);


    }
}

UserRegisterActivityEventProcessor 是你需要测试的业务
ProductCustomerRelService 是需要mock的service

你可能感兴趣的:(spring mock数据单元测试)