知识小结:
本次练习中重点关注了构造方法的打桩处理之一,以及对于包含多个参数类型的处理。
构造方法和多个参数的打桩处理:
这里根据构造方法的类型即可判断处理,并且可以利用Jtest提供的API makeStubObject(java.lang.Class class)来自动构造对象返回,这里使用该API的好处是可以针对多种复杂情况处理,可以不需要调用原构造方法来返回,或者一些未实现的接口情况,使用这种情况更容易处理。《参见Jtest Stub研究学习(1)中详解》
参考代码:大概逻辑和API调用顺序如下:
if(Stubs.matches(java.lang.Member method, java.lang.Class cl))
Class[] argument_types = new Class[]{String.class, String.class}; // 这里的String.class可能是其他只,需要考虑构造方法的参数类型填入合适值
if(Stubs.matches(method, argument_types))
return Stubs.makeStubObject(cl);
待测代码:
package com.parasoftchina.jteststub;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class ConstructorExample {
public static long getFileLength(String path) throws IOException{
RandomAccessFile file = new RandomAccessFile(path, "rw");
return file.length();
}
}
测试用例和桩代码:
/*
* ConstructorExampleTest.java
* 在16-5-20 10:31:15上被Jtest创建.
*/
package com.parasoftchina.jteststub;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import java.lang.reflect.Member;
import jtest.Stubs;
import java.io.RandomAccessFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.parasoftchina.jteststub.ConstructorExample;
/**
* ConstructorExampleTest is a test class for ConstructorExample
*
* @see com.parasoftchina.jteststub.ConstructorExample
* @author Parasoft Jtest 9.6
*/
public class ConstructorExampleTest extends PackageTestCase {
/**
* Constructor for test class.
*
* @author Parasoft Jtest 9.6
*/
public ConstructorExampleTest() {
/*
* This constructor should not be modified. Any initialization code
* should be placed in the setUp() method instead.
*/
}
/**
* Test for method getFileLength(java.lang.String).
*
* @throws Throwable
* Tests may throw any Throwable
*
* @see ConstructorExample#getFileLength(java.lang.String)
* @author Parasoft Jtest 9.6
*
*/
@Test
public void testGetFileLength0() throws Throwable {
long result = ConstructorExample.getFileLength("Mr. Bob Smith");
assertEquals(5L, result); // jtest_unverified
// No exception thrown
// jtest_unverified
}
/**
* 当运行 testGetFileLength0 方法时指定使用的桩方法。
*
* @param method
* 被调用的方法或构造方法
* @param _this
* 对应于此方法的对象实例或者 <code>null</code> 对应静态方法
* @param args
* 传递给该方法的参数
* @return 使用的桩方法的桩返回值或者 <code>Stubs.NO_STUB_GENERATED</code> 指定不应该被打桩的方法调用。
* @throws Throwable
* 桩方法可以抛出的任何异常
* @author Parasoft Jtest 9.6
* @throws FileNotFoundException
*/
public Object stubsGetFileLength0(Member method, Object _this, Object[] args) throws FileNotFoundException{
Class [] argument_types;
if(Stubs.matches(method,RandomAccessFile.class)){
argument_types = new Class[]{String.class, String.class};
if(Stubs.matches(method, argument_types)){
//return new RandomAccessFile("C:\Case\test.xml", "rw");
return Stubs.makeStubObject(RandomAccessFile.class);
}
/* argument_types = new Class[]{}; // the first way to stub length
if(Stubs.matches(method, "length", argument_types))
return new Long(5L);*/
//return Stubs.NO_STUB_GENERATED;
}
if(Stubs.matches(method, "length")) // the second way to stub length
return new Long(5L);
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 ConstructorExampleTest
*
* @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.ConstructorExampleTest");
}
/**
* 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 ConstructorExample.class;
}
}
// JTEST_CURRENT_ID=-1375974269.