知识点小结:
在本次练习中重点关注的是当测试代码中出现需要打桩的方法包含不同参数内容(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.