public class hello{
 public int sub(int m, int n){
 return m-n;
   }
 }
测试代码:
import junit tssthello.TestCase;
public class testhello extends TestCase{
private hello hello01;
public static void main(String [] args){
junit.textui.TestRunner.run(testhello.class);
}
public void testsub(){
assertEquals(hello01.sub(5,2),3);
assertEquals(hello01.sub(5,1),4);
assertEquals(hello01.sub(3,2),1);
assertEquals(hello01.sub(5,6),-1);
}
对于testsub()方法,我不想直接把值写进去,这样就是要测试一组数据就得写一句assertEquals(hello01.sub(5,2),3);太麻烦了!
我已经用Java的反射机制得到sub()方法的参数列表,返回类型,我不知道怎么与测试程序联系起来,对照得到的参数列表,返回类型,把值输到测试程序中,然后测试对不对,如果这样就不需要多个assertEquals(hello01.sub(5,2),3);语句。只要运行后输入一组值测试,完后再输入另一组值测试!!!!
帮忙看看!!!

解决方案 »

  1.   

    补充一下
    assertEquals(hello01.sub(3,2),1);
    我想将3,2,1都输进去,3,2,1的数据类型用Java的反射机制得到。
      

  2.   

    麻烦吗?楼下可以写个xml或txt等文本 然后读取里面的测试数据不就行了
    用不着反射吧。
      

  3.   

    package src;import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    import java.util.Scanner;import junit.framework.TestCase;public class testhello extends TestCase {
    private hello hello01;
    // public static void main(String [] args){
    // junit.textui.TestRunner.run(testhello.class);

    // }
    protected void setUp()throws Exception{
    super.setUp();
    hello01=new hello();
    }
    protected void tearDown()throws Exception{
    super.tearDown();
    }
         public void  testsub() throws ClassNotFoundException{
            int i;
     
         Class clazz=Class.forName("src.hello");
         System.out.println(clazz);
         Method [] methods=clazz.getDeclaredMethods();
         for(Method method: methods){
             String modifier=Modifier.toString(method.getModifiers());//获取到修饰符
             Class returnType=method.getReturnType(); //获取返回类型
             Class [] paramTypes=method.getParameterTypes();//获取参数列表
             }
         System.out.println("qing");
         Scanner r=new Scanner(System.in);
         int m=r.nextInt();
         int n=r.nextInt();
         int a=r.nextInt();
        assertEquals(hello01.sub(m,n),a);
    }
    }
    这是我写的,我想System.out.println("请输入要测试的数据类型");
    我想先确定m,n,a的类型就得用反射,然后要根据他的类型再输入对应类型的数值而不是直接用int m=r.nextInt();int类型应该是通过反射知道的,我不知道怎么输入,就是int m=r.nextInt();这句话怎么写?
      

  4.   


    for(int i=0;i<list.size();i++){
      
       assertEquals(hello01.sub(list1[i],list2[i]),list3[i]);}用list来做不行么
      

  5.   

    不是那样,得到参数类型和返回类型,提示你要给方法中输入怎么样的数据类型,输入m,n,a这三个数,这三个数就要符合参数类型和返回类型,如:
    请输入x类型的参数值,以及y类型的返回值(这里的x,y是通过反射得到,我已经做出来了)
    然后输入值,怎么输入啊?Java语言用int m=r.nextInt();之类的语句输入,但是我的输入的数据类型不确定,怎么输入?Nextint,NextDouble之类了!