tags:

views:

45

answers:

2

I wish to launch the GUI application 2 times from Java test. How should we use @annotation in this case?

public class Toto { 

@BeforeClass 
public static void setupOnce() 
{ 
final Thread thread = new Thread() { public void run() 
{ //launch appli } }; 
try { 
thread.start(); 
} catch (Exception ex) { } 
} 
public class Test extends toto {
 @Test 
public void test() {
 setuptonce(); 
closeAppli(); 
} 
@test 
public void test2() 
{ setuptonce(); 
} 
}

To launch it a second time, which annotation should I use? @afterclass?

A: 

The @BeforeClass annotation is used to run something once, before test actually runs.

So, depending on what do you want to get (and why), you can simply wrap launch code in a cycle, move launch code in other method and call it from somewhere else or write separate test case.

dchekmarev
so when I call setuponce() ,it launch the GUI once?how could I relaunch the GUI after closing it ? @testlaunch GUIclose GUIlaunch GUI
I don't know now do you launch gui, but, if it does not forks, you can write something like this:public void run() { for (int i = 0; i < 2; i++) { // launch gui }}if it forks (i.e. returns control to run() from gui before it closes), then here should be some other stuff
dchekmarev
I have one function toto.java where I define @BeforeClass with setuponce() another java test where I : -call setuponce() - @test //test1 ,close appli -@test2 //test2 ,re-call setup once and close ressource but test2 isn't done... –
+1  A: 

Method annotated with @BeforeClass means that it is run once before any of the test methods are run in the test class. Method annotated with @Before is run once before every test method in the class. The counterparts for these are @AfterClass and @After.

Probably you are aiming for something like the following.

@BeforeClass
public static void setUpClass() {
  // Initialize stuff once for ALL tests (run once)
}

@Before
public void setUp() {
  // Initialize stuff before every test (this is run twice in this example)
}

@Test
public void test1() { /* Do assertions etc. */ }

@Test
public void test2() { /* Do assertions etc. */ }

@AfterClass
public static void tearDownClass() {
  // Do something after ALL tests have been run (run once)
}

@After
public void tearDown() {
  // Do something after each test (run twice in this example)
}

You don't need to explicitly call the @BeforeClass method in your test methods, JUnit does that for you.

ponzao
I have one function toto.java where I define @BeforeClass with setuponce()another java test where I : -call setuponce()- @test //test1 ,close appli-@test2 //test2 ,re-call setup once and close ressource but test2 isn't done...
Would it be possible for you to add your test source code fully into your original post? I am having trouble understanding the problem.
ponzao
this is Toto.java :public class Toto { @BeforeClass public static void setupOnce() { final Thread thread = new Thread() { public void run() { //launch appli } }; try { thread.start(); } catch (Exception ex) { } }}this is Test.java : public class Test extends toto { @Test public void test() { setuptonce();closeAppli();}@test public void test2() { setuptonce(); }}
I upgraded my answer with a bit of information. If you have two tests and you want to initialize something before each of them, then you should use @Before public void setUp() { ... }, if you want to initialize something only once before all the tests use @BeforeClass public static void setUpClass() { ... }.
ponzao
ponzo thanks for your answerso setuponce launch the appli only once ?I have used the @Before and @After but when I run the test =>problem off initialization??
the @BeforeClass in my case is in another java file in the java test I call :@before@test@test@after
Do you need to close the tested application between tests?
ponzao
yes for the first test I need to launch and close appli for the second one two
Aah ok, then you definitely need to use @Before and @After instead of @BeforeClass and @AfterClass. @Before is run before every test and @After after every test. If you're running into problems in the second test it is probably because you were not able to close the application properly in the @After method. How are you trying to close it before restarting?
ponzao
by clicking on File menu->Exit when I try to relaunch it a second time sometimes works but open appli in the last state (not clean)
This is not really automated testing if you need user action to close the application. What are you trying to do here?
ponzao