技术开发 频道

Java私有方法属性的测试工具:Accessive

  【IT168 技术文档】1、参考

  http://code.google.com/p/accessive/

  2、详细使用说明

  http://code.google.com/p/accessive/wiki/Documentation

  3、java基本类型的属性的测试

  3.1 业务对象类

package com.wdzwdz;

  
public class AnObject {

  
private int anInt;

  
private String aString;

  
private String aMethod(int value) {

  
return "wdz";

  }

  
private String anotherMethod(Integer a, int b, float c) {

  
return "wdz" + a + "," + b + "," + c;

  }

  
public AnObject() {

  };

  
public AnObject(int a, String s) {

  anInt
= a;

  aString
= s;

  };

  }

  3.2 业务对象的私有属性的测试

  注意例子中的私有属性是java基本类型(int),而非包裹类(Integer);测试时使用了对应的包裹类 

package com.wdzwdz.test;

  
import junit.framework.Assert;

  
import org.junit.AfterClass;

  
import org.junit.Before;

  
import org.junit.BeforeClass;

  
import org.junit.Test;

  
import com.j2speed.accessor.FieldAccessor;

  
import com.wdzwdz.AnObject;

  
public class AnObjectTest {

  
private AnObject target;

  @BeforeClass

  
public static void setUpBeforeClass() throws Exception {

  }

  @AfterClass

  
public static void tearDownAfterClass() throws Exception {

  }

  @Before

  
public void setUp() throws Exception {

  target
= new AnObject(10, "wdz1");

  }

  @Test

  
//基本类型的属性,需要使用对应的包裹类来指定

  
public void testAnInt() {

  FieldAccessor anInt
= new FieldAccessor(

  
"anInt", AnObject.class);

  Integer i
= anInt.get(target);

  Assert.assertEquals(
new Integer(10), i);

  }

  @Test

  
public void testAnObjectIntString() {

  FieldAccessor h
= new FieldAccessor ("aString",AnObject.class);

  String act
= h.get(target);

  Assert.assertEquals(
"wdz1", act);

  }

  }

  4、私有方法的测试

  私有方法的参数带有包裹类型(Integer),基本类型(int)

package com.wdzwdz.test;

  
import junit.framework.Assert;

  
import org.junit.AfterClass;

  
import org.junit.Before;

  
import org.junit.BeforeClass;

  
import org.junit.Test;

  
import com.j2speed.accessor.MethodAccessor;

  
import com.wdzwdz.AnObject;

  
/****

  * test methord

  *
*/

  
public class AnObjectTest2 {

  
private AnObject target;

  @BeforeClass

  
public static void setUpBeforeClass() throws Exception {

  }

  @AfterClass

  
public static void tearDownAfterClass() throws Exception {

  }

  @Before

  
public void setUp() throws Exception {

  target
= new AnObject(10, "wdz1");

  }

  @Test

  
public void testanotherMethod() {

  MethodAccessor m
= new MethodAccessor("anotherMethod",

  target, Integer.
class, int.class, float.class);

  String s
= m.invoke(new Integer(1), 2, 1.1f);

  System.err.println(s);

  Assert.assertEquals(
"wdz1,2,1.1", s);

  }

  }
0
相关文章