junit4 函数
如何创建可以从每个 java 测试中调用的通用函数? 在我的函数 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) {
}
}
}
在 toto.java 中,我按如下方式调用该函数:startappli.testmain(loginfo) 它不起作用 帮助 ?
我的函数:Runner.java 包含: 公共类 RunAppli {
@BeforeClass 公共静态无效setupOnce(){ 最终线程线程=新线程(){ 公共无效运行(){
Main.main(new String[]{"-rtebase ", "C:\\bin"});
}
};
try {
thread.start();
} catch (Exception ex) {
}
}
@测试 公共无效测试(){
URL path = this.getClass().getResource("../Tester/map.xml");
System.out.println("Cover: " + cmapURL);
}
}
}
从我的 java 测试 TestDemo.java 中,我调用 StartAppli 来启动 appli GUI: RunAppli .setupOnce(); 我得到了 xml 文件的路径: 运行应用程序.path 我们应该在函数中使用@Test吗? 有什么建议吗? 谢谢
how to create generic functions that could be called from each java test?
In my function startappli I have :
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 ?
my function: Runner.java contains :
public class RunAppli {
@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() {
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();
and I get the path to xml file :
RunAppli .path
should we use @Test in functions ?
any suggession ?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您的问题是
startappli.setupOnce()
未执行。我相信这是因为
startappli
不包含@Test
方法,因此 JUnit 不将其作为测试类。因此,@BeforeClass
被省略,该函数不会由 JUnit 执行。解决方案可能是在此类中放置一个
@Test
方法。或者,如果您希望在每个 Java 测试方法之前调用它,则应该从每个测试类中的@Before
方法(或从@BeforeClass
> 方法(如果您希望每个测试类只运行一次)。注意:
startappli
对于 Java 类来说不是一个好名字,因为约定类名应该采用驼峰式大小写,例如StartAppli
。I assume your problem is that
startappli.setupOnce()
is not executed.I believe this is because
startappli
contains no@Test
methods, therefore JUnit does not take it as a test class. Thus@BeforeClass
is omitted and the function is not executed by JUnit.A solution could be to put a
@Test
method in this class. 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).Note:
startappli
is not a good name for a Java class, as the convention is that class names should be camel case, e.g.StartAppli
.你做错了很多事情(实际上 - 全部)
Java 命名约定 规定类名应大写
您在方法采用驼峰命名法时调用
testmain()
和main()
-testMain()
.您不应该使用 JUnit 在新线程中运行 main 方法。 JUnit 运行程序负责实例化。
您的测试类应该执行测试 - 即它应该有一个用
@Test
注释的方法我建议在开始之前阅读 JUnit 手册。
You are doing a number of things wrong (actually - all)
The Java Naming Conventions say that class names should be uppercase
You are calling
testmain()
andmain()
while the method is camelCased -testMain()
.You should not run a main method in a new thread with JUnit. A JUnit runner takes care of the instantiation.
Your testclass should perform tests - i.e. it should have a method annotated with
@Test
I'd suggest reading the JUnit manual before starting.