java UT
Posted eaglediao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java UT相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章