junit与testng 分别和mockito 结合使用例子

pom文件 引入:


    org.testng
     testng
     6.8.8
     test


使用junit:

@RunWith(MockitoJUnitRunner.class)  
public class MockTest {
    
    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
    }
    
    @InjectMocks
    TestSendService testSendService = new TestSendServiceImpl();
    
    @InjectMocks
    NoticeTSendProxy noticeTSendProxy;
    
    @Test
    public void mockTest() {
        Map paramMap = new HashMap();
        paramMap.put("source", From.JBP.from());
        noticeTSendProxy.testSendSms(paramMap, getOrderMappingDTO(ChannelStore.SMS.toString(), JBPTestSendCommandType.TEST_SNED_ADVICE_COMMAND,
		"单元测试()", "18512581771"));
//        testSendService.testSend4Jbp(getOrderMappingDTO(ChannelStore.SMS.toString(), JBPTestSendCommandType.TEST_SNED_ADVICE_COMMAND, "单元测试()", "18512581771"));
    }
    
    private OrderMappingDTO getOrderMappingDTO(String channelCode, String orderType, String content, String mobiles) {
        OrderMappingDTO orderMappingDTO = new OrderMappingDTO();
        orderMappingDTO.setChannel(channelCode);
        orderMappingDTO.setOrderType(orderType);
        orderMappingDTO.setSmsContent(content);
        orderMappingDTO.setMobile(mobiles);
        return orderMappingDTO;
    }
}

使用testng:

public class OrderSendServiceTest extends BaseTest {

//    @Autowired
//    TestSendService testSendService;
	 @InjectMocks
	 private TestSendService testSendService;
	 
	 @BeforeClass
	 public void beforeClass() {
	        // 自动将依赖的类注入待测类
	    MockitoAnnotations.initMocks(this);
	 }
    
     @Test
     public void orderSendTest() {
        // Assert.assertNotNull(testSendService, "Create orderSendService instance Fail.");
        
        // 测试发送短信
        testSendService.testSend4Jbp(getOrderMappingDTO(ChannelStore.SMS.toString(),
        		JBPTestSendCommandType.TEST_SNED_ADVICE_COMMAND, "单元测试()", "18512581771"));
   
        // 测试发送PUSH
        testSendService.testSend4Jbp(getOrderMappingDTO(ChannelStore.SMS.toString(), 
        		JBPTestSendCommandType.TEST_SNED_ADVICE_COMMAND, "单元测试()", "18512581771"));
     }
    
   
    /***
     * 待发送的数据
     * @param channelCode
     * @param orderType
     * @param content
     * @param mobiles
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private OrderMappingDTO getOrderMappingDTO(String channelCode, String orderType, String content, String mobiles) {
        OrderMappingDTO orderMappingDTO = new OrderMappingDTO();
        orderMappingDTO.setChannel(channelCode);
        orderMappingDTO.setOrderType(orderType);
        orderMappingDTO.setSmsContent(content);
        orderMappingDTO.setMobile(mobiles);
        return orderMappingDTO;
    }
}





你可能感兴趣的:(单元测试与powerMock)