技术开发 频道

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);

  }

  }
0
相关文章