0

我正在尝试制作一个测试运行器,第一个类是测试运行器,第二个是 testet 类,最后一个是我的主页。我想使用方法调用来访问测试器方法,但这不起作用。

    public class tester {

        public tester()
        {

        }

        @Test
        public void testTopla(){
            int sayi1 = 10;
            int sayi2 = 20;
            int gercekSonuc = 30;
            //
            islem isl = new islem(sayi1, sayi2);
            int sonuc = isl.topla();
            Assert.assertTrue("Sonucta Hata Var", sonuc == gercekSonuc);
        }

    and this is what i am testing
    public class islem {
        private int sayi1;
        private int sayi2;

        public islem(){

        }
        public islem(int sayi1, int sayi2){
            this.sayi1 = sayi1;
            this.sayi2 = sayi2;
        }

        public int getSayi1() {
            return sayi1;
        }

        public void setSayi1(int sayi1) {
            this.sayi1 = sayi1;
        }

        public int getSayi2() {
            return sayi2;
        }

        public void setSayi2(int sayi2) {
            this.sayi2 = sayi2;
        }
        public int topla(){
            return this.sayi1 + this.sayi2;
        }


    and this
public class Deneme1UI extends UI {

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = Deneme1UI.class)
    public static class Servlet extends VaadinServlet {
    }

    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();

        layout.setMargin(true);
        setContent(layout);

     Class hilmi = tester.class;

       tester obj = new tester();


        for(Method method : hilmi.getDeclaredMethods())
        {   
            method.invoke(obj);

    }
4

1 回答 1

1

看看这个: http ://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html#invoke%28java.lang.Object,%20java.lang.Object...% 29

invoke() 函数的第一个参数需要是反射类的实例,在您的情况下是类的实例tester

您必须先创建tester该类的实例,然后才能调用其方法

于 2013-08-05T07:59:19.880 回答