Java Stub 研究学习(2)
生活随笔
收集整理的這篇文章主要介紹了
Java Stub 研究学习(2)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
知識點小結: 在本次練習中重點關注的是當測試代碼中出現需要打樁的方法包含不同參數內容(1個參數)時的靈活打樁處理。 如代碼中提到的file.compareTo(String name)方法,其中name可能為不同的值,如果需要在樁中根據name的值來靈活打樁,應該如何處理呢? 方法就是需要進行參數比對。在樁方法的查找匹配過程中,需要考慮args參數的利用。 樁聲明:public Object stubsAnalyze0(java.lang.reflect.Member, Object _this, Object[] args) ? ? 其中存在一個參數項: Object[] args 。需要提取這個args數據并進行必要的判斷處理,如下所示: ?? if(Stubs.matches( method, “compareTo”)){ File file = (File) args[0]; //這里以compareTo為例,其中參數為一個File類型。 if(file.getName().equals(name)) return new Integer(100); return new Integer(-1); } 被測代碼: package com.parasoftchina.jteststub; ? import java.io.File; ? public class FileExample { ? ? ? public static String analyze (File file){ ? ? ? ? StringBuffer tmp = new StringBuffer(); ? ? ? ? ? tmp.append(file.getAbsolutePath()); ? ? ? ? tmp.append(':'); ? ? ? ? tmp.append(file.setLastModified(100L)); ? ? ? ? tmp.append(':'); ? ? ? ? tmp.append(file.compareTo(new File("X001.txt"))); ? ? ? ? tmp.append(':'); ? ? ? ? tmp.append(file.compareTo(new File("XXX"))); ? ? ? ? tmp.append(':'); ? ? ? ? ? return tmp.toString(); ? ? } } 測試用例和樁代碼: /* * FileExampleTest.java * 在16-5-20 9:51:27上被Jtest創建. */ package com.parasoftchina.jteststub; import jtest.Report; import jtest.Stubs; import java.lang.reflect.Member; import org.junit.Before; import org.junit.After; import org.junit.Test; import com.parasoftchina.jteststub.FileExample; import java.io.File; /** * FileExampleTest is a test class for FileExample * * @see com.parasoftchina.jteststub.FileExample * @author Parasoft Jtest 9.6 */ public class FileExampleTest extends PackageTestCase { /** * Constructor for test class. * * @author Parasoft Jtest 9.6 */ public FileExampleTest() { /* * This constructor should not be modified. Any initialization code * should be placed in the setUp() method instead. */ } /** * Test for method analyze(java.io.File). *? * @throws Throwable *? ? ? ? ? ? ?Tests may throw any Throwable * * @see FileExample#analyze(java.io.File) * @author Parasoft Jtest 9.6 *? */ @Test public void testAnalyze0() throws Throwable { File file = new File("file"); String result = FileExample.analyze(file); Report.submitMessage(result); } /** * 當運行 testAnalyze0 方法時指定使用的樁方法。 * @param method 被調用的方法或構造方法 * @param _this 對應于此方法的對象實例或者 *? ? ? ? <code>null</code> 對應靜態方法 * @param args 傳遞給該方法的參數 * @return 使用的樁方法的樁返回值或者 <code>Stubs.NO_STUB_GENERATED</code> *? ? ? ? 指定不應該被打樁的方法調用。 * @throws Throwable 樁方法可以拋出的任何異常 * @author kwang */ public Object stubsAnalyze0(Member method, Object _this, Object[] args) throws Throwable { if(Stubs.matches(method, File.class)){ if(Stubs.matches(method,"getAbsolutePath")) return "C:\\Case\\a.txt"; if(Stubs.matches(method, "setLastModified")){ long time = ((Long)args[0]).longValue(); if(time<0) throw new IllegalArgumentException("time is "+ time); return new Boolean(true); } if(Stubs.matches(method, "compareTo")){ File file = (File) args[0]; if(file.getName().equals("X001.txt")) return new Integer(100); else? return new Integer(0); } return Stubs.NO_STUB_GENERATED; } return Stubs.NO_STUB_GENERATED; } /** * Used to set up the test. This method is called by JUnit before each of * the tests are executed. *? * @author Parasoft Jtest 9.6 */ @Before public void setUp() throws Exception { /* * Add any necessary initialization code here (e.g., open a socket). * Call Repository.putTemporary() to provide initialized instances of * objects to be used when testing. */ super.setUp(); // jtest.Repository.putTemporary("name", object); } /** * Used to clean up after the test. This method is called by JUnit after * each of the tests have been completed. *? * @author Parasoft Jtest 9.6 */ @After public void tearDown() throws Exception { try { /* * Add any necessary cleanup code here (e.g., close a socket). */ } finally { super.tearDown(); } } /** * Utility main method. Runs the test cases defined in this test class. *? * Usage: java FileExampleTest *? * @param args *? ? ? ? ? ? command line arguments are not needed * @author Parasoft Jtest 9.6 */ public static void main(String[] args) { // junit.textui.TestRunner will print the test results to stdout. org.junit.runner.JUnitCore.main("com.parasoftchina.jteststub.FileExampleTest"); } /** * Get the class object of the class which will be tested. *? * @return the class which will be tested * @author Parasoft Jtest 9.6 */ public Class getTestedClass() { return FileExample.class; } } // JTEST_CURRENT_ID=863432002.
轉載于:https://www.cnblogs.com/kwang-cai/p/5548801.html
總結
以上是生活随笔為你收集整理的Java Stub 研究学习(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我眼中的技术地图
- 下一篇: 怎样找到所有相同的数据,在Excel表中