Reputation: 1351
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
Upvotes: 0
Views: 478
Reputation: 597324
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()
and main()
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.
Upvotes: 0
Reputation: 116296
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
.
Upvotes: 0