eclipse进行Junit测试

使用JunitTest测试接口,目标:测试TradeToolContextService的getTradeToolByChannelCode方法能否正常返回。



1.引入junit依赖。


            junit
            junit
            4.1.11
            test
        

2.生成对应的测试类。在要测试的类上右键-New-Junit Test Case。



import org.junit.Test;

public class TradeToolContextServiceTest2 {

    @Test
    public void testGetTradeToolByChannelCode() {
        fail("Not yet implemented");
    }

}

3.编写测试类。

import javax.annotation.Resource;

import org.junit.Test;
import org.springframework.util.Assert;

import com.metersbonwe.oms.core.service.impl.tradetool.VipTradeToolServiceImpl;

public class TradeToolContextServiceTest2 {

    @Resource
    private TradeToolContextService tradeToolContextService;

    @Test
    public void testGetTradeToolByChannelCode() {
        TradeToolService res = tradeToolContextService.getTradeToolByChannelCode("WPH_CHANNEL_CODE");
        Assert.isTrue(!(res instanceof VipTradeToolServiceImpl),"比对不一致");
    }
}

4.运行测试方法。
在Assert.isTrue语句打上断点,testGetTradeToolByChannelCode()方法上右键-Debug As-Junit Test。
运行报空指针,忘记把spring容器配置文件和Junit运行注解加上了,修改测试类。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;

import com.metersbonwe.oms.core.service.impl.tradetool.VipTradeToolServiceImpl;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/spring-Context.xml","classpath:spring/dispatcher-servlet.xml" })
public class TradeToolContextServiceTest2 {

    @Resource
    private TradeToolContextService tradeToolContextService;

    @Test
    public void testGetTradeToolByChannelCode() {
        TradeToolService res = tradeToolContextService.getTradeToolByChannelCode("WPH_CHANNEL_CODE");
        Assert.isTrue(!(res instanceof VipTradeToolServiceImpl),"比对不一致");
    }
}

重复上面的步骤。



那么下面的语句就会报错。最后看执行结果:



DONE!

你可能感兴趣的:(eclipse进行Junit测试)