码迷,mamicode.com
首页 > 编程语言 > 详细

java UT

时间:2017-09-08 09:56:54      阅读:385      评论:0      收藏:0      [点我收藏+]

标签:实现   unit   ref   com   imp   link   path   orm   prepare   

mock 构造函数

import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.tdtech.eplatform.gatekeeper.config.entity.Listenexternal;
import com.tdtech.eplatform.gatekeeper.constant.IConfigFileSet;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ExcelParasForListener.class)
/*
 * About @PowerMockIgnore:
 * http://www.ryanchapin.com/fv-b-4-816/Java-PowerMock-Could-not-reconfigure-JMX-java-lang-LinkageError-Solution.html
 */
@PowerMockIgnore({ "javax.management.*" })
public class ExcelParasForListenerTest {

    private ExcelParasForListener excelParasForListener = new ExcelParasForListener();

    @Test
    public void testLoadIPlistenerConfig() throws Exception {
        // mock ExcelReader constructor
        ExcelReader mocIPexcelReader = new ExcelReader(IConfigFileSet.easyconfTemplatePath,
                IConfigFileSet.sheetNameofListenIPMapping);
        PowerMockito.whenNew(ExcelReader.class)
                .withArguments(IConfigFileSet.importEasyconfFilePath, IConfigFileSet.sheetNameofListenIPMapping)
                .thenReturn(mocIPexcelReader);
        // test and verify
        ArrayList<Listenexternal> loadIPlistenerConfig = excelParasForListener.loadIPlistenerConfig();
        assertEquals("10.10.10.11", loadIPlistenerConfig.get(0).getListenIP());
        assertEquals(2404, loadIPlistenerConfig.get(0).getListenPort());
    }
}

可参考"如何使用PowerMock和Mockito来mock 1. 构造函数 2. 静态函数 3. 枚举实现的单例 4. 选择参数值做为函数的返回值":

http://www.blogjava.net/usherlight/archive/2015/06/16/425740.html

java UT

标签:实现   unit   ref   com   imp   link   path   orm   prepare   

原文地址:http://www.cnblogs.com/eaglediao/p/7492810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!