简体   繁体   English

junit4函数

[英]junit4 functions

how to create generic functions that could be called from each java test? 如何创建可以从每个Java测试调用的泛型函数? In my function startappli I have : 在我的函数startappli中,我有:

public class startappli{

public void testMain (String[] args) 
 {

  String[] logInfos = new String[3];
  logInfos[0] = (String) args[0];
  logInfos[1] = (String) args[1];
 }
  @BeforeClass
    public static void setupOnce() {
        final Thread thread = new Thread() {
            public void run() {

             entrypointtoGUI.main(new String[]{"arg0 ", "arg1"});

            }
        };
        try {
            thread.start();
        } catch (Exception ex) {

        }
    }

}

in the toto.java , I call the function as follow : startappli.testmain(loginfo) it doesn't work help ? 在toto.java中,我按如下方式调用该函数:startappli.testmain(loginfo)它不起作用吗?


my function: Runner.java contains : public class RunAppli { 我的功能:Runner.java包含:公共类RunAppli {

@BeforeClass public static void setupOnce() { final Thread thread = new Thread() { public void run() { @BeforeClass public static void setupOnce(){final thread thread = new Thread(){public void run(){

            Main.main(new String[]{"-rtebase ", "C:\\bin"});

        }
    };
    try {
        thread.start();
    } catch (Exception ex) {

    }
}

@Test public void test() { @Test public void test(){

    URL path = this.getClass().getResource("../Tester/map.xml");


    System.out.println("Cover: " + cmapURL);




    }

}

} }
and from my java test TestDemo.java , I call StartAppli THAT launch the appli GUI : RunAppli .setupOnce(); 从我的Java测试TestDemo.java中,我调用StartAppli来启动应用程序GUI:RunAppli .setupOnce(); and I get the path to xml file : RunAppli .path should we use @Test in functions ? 我得到xml文件的路径:RunAppli .path我们是否应该在函数中使用@Test? any suggession ? 有什么建议吗? thanks 谢谢

I assume your problem is that startappli.setupOnce() is not executed. 我认为您的问题是startappli.setupOnce()未执行。

I believe this is because startappli contains no @Test methods, therefore JUnit does not take it as a test class. 我相信这是因为startappli包含@Test方法,因此JUnit不会将其作为测试类。 Thus @BeforeClass is omitted and the function is not executed by JUnit. 因此,@ @BeforeClass被省略,并且该函数不由JUnit执行。

A solution could be to put a @Test method in this class. 一个解决方案可能是在此类中添加@Test方法。 Or, if you want it to be called before each Java test method, you should call it explicitly from the @Before methods in each of your test classes (or from the @BeforeClass methods if you want it to run only once for each test class). 或者,如果希望在每个Java测试方法之前调用它,则应从每个测试类中的@Before方法显式调用它(如果希望每个测试类仅运行一次,则从@BeforeClass方法显式调用它。 )。

Note: startappli is not a good name for a Java class, as the convention is that class names should be camel case, eg StartAppli . 注意: startappli不是Java类的好名字,因为约定是类名应为驼峰式,例如StartAppli

You are doing a number of things wrong (actually - all) 您做错了很多事(实际上-全部)

  1. The Java Naming Conventions say that class names should be uppercase Java命名约定说,类名应为大写

  2. You are calling testmain() and main() while the method is camelCased - testMain() . 方法为camelCased- testMain()时,您正在调用testmain()main() testMain()

  3. You should not run a main method in a new thread with JUnit. 您不应该使用JUnit在新线程中运行main方法。 A JUnit runner takes care of the instantiation. JUnit运行器负责实例化。

  4. Your testclass should perform tests - ie it should have a method annotated with @Test 您的testclass应该执行测试-即它应该有一个用@Test注释的方法

I'd suggest reading the JUnit manual before starting. 我建议您在开始之前阅读JUnit手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM