美文网首页
spring mock数据单元测试

spring mock数据单元测试

作者: 团长plus | 来源:发表于2019-12-13 17:15 被阅读0次

    依赖的pom

    <dependency>
                <groupId>org.evosuite</groupId>
                <artifactId>evosuite-standalone-runtime</artifactId>
                <version>1.0.6</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-api-mockito</artifactId>
                <version>1.7.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-module-junit4</artifactId>
                <version>1.7.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.javassist</groupId>
                <artifactId>javassist</artifactId>
                <version>3.18.2-GA</version>
            </dependency>
    
    

    代码

    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数据单元测试

          本文链接:https://www.haomeiwen.com/subject/yitqnctx.html