Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

junit annotation

Tags:

junit4

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?

like image 903
user281070 Avatar asked Mar 18 '10 20:03

user281070


1 Answers

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.

like image 174
ponzao Avatar answered Oct 30 '22 14:10

ponzao