其中重点需要注意几个技术点:
- 函数调用计数: jtest.StubIterationCounter 类中声明了计数器。
其中的方法如下所示可以用来获取统计函数的调用次数:
int getIterationCount(java.lang.String method_id) —— Increment and retrieve the iteration count for a stubbed method.
通常使用方法:
a. 首先调用构造函数定义一个计数器;
protected static test.StubIterationCounter _stub_counter = new StubIterationCounter();
b. 其次调用getIterationCount(java.lang.String method_id)统计;
int index = _stub_counter.getIterationCount(“java.io.DataInputStream.readInt()”);
c. 通过使用switch方式分别返回不同结果。
if(Stubs.matches(member, “readInt”)){
int index = _stub_counter.getIterationCount(“java.io.DataInputStream.readInt()”);
switch(index){
case 1:
return new Integer(2);
case 2:
return new Integer(3);
default:
return Stubs.NO_STUB_GENERATED;
}
}
- Stubs.mathes的直接运用 — Stubs.matches(java.lang.reflect.Member member, String method_name)
- 对于整个类的打桩和对于单个测试用例打桩方法的使用:
a. 整个类的打桩: public Object Stubs(java.lang.reflect.Memeber member, Object _this, Object[] args);
b. 单个测试用例的打桩: public Object StubsgetNext1(java.lang.reflect.Member member, Object _this, Object[] args);
- 桩中构造函数的处理 : Jtest的API : makeStubObjec — public static java.lang.Object makeStubObject(java.lang.Class cl)
a. 可以用来构造对象 — 不使用原构造函数;
b. 用于构造那些接口interface的对象,因为有些interface可能还没有被任何的类实现,当测试这些接口的时候,可以使用该API来构造对象。
- 对于打桩的方法除了stub.mathes的运用外,其实可以直接使用如下形式:
public Object stubsgetNext(java.lang.reflect.Member member){
String methodName = member.getName();
if(methodName.equals(“readUTF"))
return “ADD”;
if(methodName.equals(“readInt"))
return new Integer(10);
} - 注意事项:桩的返回值必须是一个对象,所以即使需要返回一个整型数或浮点数即基本类型的数据时,需要使用对应的数据类形式,如int 对应的是Integer,double对应的是Double等等。
实际被测代码:
package com.parasoftchina.jteststub;
import java.io.*;
public class Interpreter
{
public Interpreter (InputStream stream) {
_dataInputStream = new DataInputStream (stream);
}
public Integer getNext ()
throws IOException
{
String command = _dataInputStream.readUTF ();
int op1 = _dataInputStream.readInt ();
int op2 = _dataInputStream.readInt ();
if (command.equals ("ADD"))
return new Integer (op1 - op2); // BUG: should be "+"
else if (command.equals ("SUB"))
return new Integer (op1 - op2);
return null;
}
private DataInputStream _dataInputStream;
}
测试用例代码:
/*
* InterpreterTest.java
* 在16-5-19 15:38:22上被Jtest创建.
*/
package com.parasoftchina.jteststub;
import jtest.Stubs;
import java.lang.reflect.Member;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import java.io.InputStream;
import com.parasoftchina.jteststub.Interpreter;
import jtest.Repository;
/**
* InterpreterTest is a test class for Interpreter
*
* @see com.parasoftchina.jteststub.Interpreter
* @author Parasoft Jtest 9.6
*/
public class InterpreterTest extends PackageTestCase {
protected static jtest.StubIterationCounter _stub_iteration = null;
/**
* Constructor for test class.
*
* @author Parasoft Jtest 9.6
*/
public InterpreterTest() {
/*
* This constructor should not be modified. Any initialization code
* should be placed in the setUp() method instead.
*/
}
/**
* Test for method Interpreter(java.io.InputStream).
*
* @throws Throwable
* Tests may throw any Throwable
*
* @see Interpreter#Interpreter(java.io.InputStream)
* @author Parasoft Jtest 9.6
*
*/
@Test
public void testInterpreter0() throws Throwable {
//InputStream stream = (InputStream) Repository.getObject(InputStream.class, "inputstream");
InputStream stream = (InputStream) jtest.JT.makeStubObject (InputStream.class);
Interpreter testedObject = new Interpreter(stream);
// jtest.NoSuchValueException thrown
// jtest_unverified
}
@Test
public void testgetNext1() throws Throwable {
//InputStream stream = (InputStream) Repository.getObject(InputStream.class, "inputstream");
InputStream stream = (InputStream) jtest.JT.makeStubObject (InputStream.class);
Interpreter testedObject = new Interpreter(stream);
testedObject.getNext();
// jtest.NoSuchValueException thrown
// jtest_unverified
}
@Test
public void testgetNext2() throws Throwable {
//InputStream stream = (InputStream) Repository.getObject(InputStream.class, "inputstream");
InputStream stream = (InputStream) jtest.JT.makeStubObject (InputStream.class);
Interpreter testedObject = new Interpreter(stream);
testedObject.getNext();
// jtest.NoSuchValueException thrown
// jtest_unverified
}
public Object stubsgetNext2(Member method, Object _this, Object[] args) throws Throwable {
// TODO Auto-generated stub template
if (Stubs.matches(method, "readInt")) {
int countCall = _stub_iteration.getIterationCount("java.io.DataInputStream.readInt()");
switch(countCall){
* InterpreterTest.java
* 在16-5-19 15:38:22上被Jtest创建.
*/
package com.parasoftchina.jteststub;
import jtest.Stubs;
import java.lang.reflect.Member;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import java.io.InputStream;
import com.parasoftchina.jteststub.Interpreter;
import jtest.Repository;
/**
* InterpreterTest is a test class for Interpreter
*
* @see com.parasoftchina.jteststub.Interpreter
* @author Parasoft Jtest 9.6
*/
public class InterpreterTest extends PackageTestCase {
protected static jtest.StubIterationCounter _stub_iteration = null;
/**
* Constructor for test class.
*
* @author Parasoft Jtest 9.6
*/
public InterpreterTest() {
/*
* This constructor should not be modified. Any initialization code
* should be placed in the setUp() method instead.
*/
}
/**
* Test for method Interpreter(java.io.InputStream).
*
* @throws Throwable
* Tests may throw any Throwable
*
* @see Interpreter#Interpreter(java.io.InputStream)
* @author Parasoft Jtest 9.6
*
*/
@Test
public void testInterpreter0() throws Throwable {
//InputStream stream = (InputStream) Repository.getObject(InputStream.class, "inputstream");
InputStream stream = (InputStream) jtest.JT.makeStubObject (InputStream.class);
Interpreter testedObject = new Interpreter(stream);
// jtest.NoSuchValueException thrown
// jtest_unverified
}
@Test
public void testgetNext1() throws Throwable {
//InputStream stream = (InputStream) Repository.getObject(InputStream.class, "inputstream");
InputStream stream = (InputStream) jtest.JT.makeStubObject (InputStream.class);
Interpreter testedObject = new Interpreter(stream);
testedObject.getNext();
// jtest.NoSuchValueException thrown
// jtest_unverified
}
@Test
public void testgetNext2() throws Throwable {
//InputStream stream = (InputStream) Repository.getObject(InputStream.class, "inputstream");
InputStream stream = (InputStream) jtest.JT.makeStubObject (InputStream.class);
Interpreter testedObject = new Interpreter(stream);
testedObject.getNext();
// jtest.NoSuchValueException thrown
// jtest_unverified
}
public Object stubsgetNext2(Member method, Object _this, Object[] args) throws Throwable {
// TODO Auto-generated stub template
if (Stubs.matches(method, "readInt")) {
int countCall = _stub_iteration.getIterationCount("java.io.DataInputStream.readInt()");
switch(countCall){
case 1:
return new Integer(3);
case 2:
return new Integer(2);
default :
return Stubs.NO_STUB_GENERATED;
}
}
if (Stubs.matches(method, "readUTF")){
/* int index = _stub_iteration.getIterationCount("java.io.DataInputStream.readUTF()");
switch(index){
case 1:
return "ADD";
case 2:
return "SUB";
default:
return Stubs.NO_STUB_GENERATED;
}*/
return "SUB";
}
return Stubs.NO_STUB_GENERATED;
}
@Test
public void testgetNext3() throws Throwable {
//InputStream stream = (InputStream) Repository.getObject(InputStream.class, "inputstream");
InputStream stream = (InputStream) jtest.JT.makeStubObject (InputStream.class);
Interpreter testedObject = new Interpreter(stream);
testedObject.getNext();
// jtest.NoSuchValueException thrown
// jtest_unverified
}
public Object stubsgetNext3(Member method, Object _this, Object[] args) throws Throwable {
// TODO Auto-generated stub template
if (Stubs.matches(method, "readInt")) {
int countCall = _stub_iteration.getIterationCount("java.io.DataInputStream.readInt()");
switch(countCall){
return Stubs.NO_STUB_GENERATED;
}
}
if (Stubs.matches(method, "readUTF")){
/* int index = _stub_iteration.getIterationCount("java.io.DataInputStream.readUTF()");
switch(index){
case 1:
return "ADD";
case 2:
return "SUB";
default:
return Stubs.NO_STUB_GENERATED;
}*/
return "SUB";
}
return Stubs.NO_STUB_GENERATED;
}
@Test
public void testgetNext3() throws Throwable {
//InputStream stream = (InputStream) Repository.getObject(InputStream.class, "inputstream");
InputStream stream = (InputStream) jtest.JT.makeStubObject (InputStream.class);
Interpreter testedObject = new Interpreter(stream);
testedObject.getNext();
// jtest.NoSuchValueException thrown
// jtest_unverified
}
public Object stubsgetNext3(Member method, Object _this, Object[] args) throws Throwable {
// TODO Auto-generated stub template
if (Stubs.matches(method, "readInt")) {
int countCall = _stub_iteration.getIterationCount("java.io.DataInputStream.readInt()");
switch(countCall){
case 1:
return new Integer(3);
case 2:
return new Integer(2);
default :
return Stubs.NO_STUB_GENERATED;
}
}
if (Stubs.matches(method, "readUTF")){
/* int index = _stub_iteration.getIterationCount("java.io.DataInputStream.readUTF()");
switch(index){
case 1:
return "ADD";
case 2:
return "SUB";
default:
return Stubs.NO_STUB_GENERATED;
}*/
return "test";
}
return Stubs.NO_STUB_GENERATED;
}
/**
* 当运行 testgetNext 方法时指定使用的桩方法。
* @param method 被调用的方法或构造方法
* @param _this 对应于此方法的对象实例或者
* <code>null</code> 对应静态方法
* @param args 传递给该方法的参数
* @return 使用的桩方法的桩返回值或者 <code>Stubs.NO_STUB_GENERATED</code>
* 指定不应该被打桩的方法调用。
* @throws Throwable 桩方法可以抛出的任何异常
* @author kwang
*/
public Object stubs(Member method, Object _this, Object[] args) throws Throwable {
// TODO Auto-generated stub template
if (Stubs.matches(method, "readInt")) {
int countCall = _stub_iteration.getIterationCount("java.io.DataInputStream.readInt()");
switch(countCall){
return Stubs.NO_STUB_GENERATED;
}
}
if (Stubs.matches(method, "readUTF")){
/* int index = _stub_iteration.getIterationCount("java.io.DataInputStream.readUTF()");
switch(index){
case 1:
return "ADD";
case 2:
return "SUB";
default:
return Stubs.NO_STUB_GENERATED;
}*/
return "test";
}
return Stubs.NO_STUB_GENERATED;
}
/**
* 当运行 testgetNext 方法时指定使用的桩方法。
* @param method 被调用的方法或构造方法
* @param _this 对应于此方法的对象实例或者
* <code>null</code> 对应静态方法
* @param args 传递给该方法的参数
* @return 使用的桩方法的桩返回值或者 <code>Stubs.NO_STUB_GENERATED</code>
* 指定不应该被打桩的方法调用。
* @throws Throwable 桩方法可以抛出的任何异常
* @author kwang
*/
public Object stubs(Member method, Object _this, Object[] args) throws Throwable {
// TODO Auto-generated stub template
if (Stubs.matches(method, "readInt")) {
int countCall = _stub_iteration.getIterationCount("java.io.DataInputStream.readInt()");
switch(countCall){
case 1:
return new Integer(3);
case 2:
return new Integer(2);
default :
return Stubs.NO_STUB_GENERATED;
}
}
if (Stubs.matches(method, "readUTF")){
int index = _stub_iteration.getIterationCount("java.io.DataInputStream.readUTF()");
switch(index){
case 1:
return "ADD";
case 2:
return "SUB";
default:
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();
_stub_iteration = new jtest.StubIterationCounter();
// 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 InterpreterTest
*
* @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.InterpreterTest");
}
/**
* 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 Interpreter.class;
}
}
return new Integer(2);
default :
return Stubs.NO_STUB_GENERATED;
}
}
if (Stubs.matches(method, "readUTF")){
int index = _stub_iteration.getIterationCount("java.io.DataInputStream.readUTF()");
switch(index){
case 1:
return "ADD";
case 2:
return "SUB";
default:
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();
_stub_iteration = new jtest.StubIterationCounter();
// 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 InterpreterTest
*
* @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.InterpreterTest");
}
/**
* 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 Interpreter.class;
}
}
// JTEST_CURRENT_ID=-740272087.