Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't my @BeforeClass method running?

I have the following code:

    @BeforeClass     public static void setUpOnce() throws InterruptedException {         fail("LOL");     } 

And various other methods that are either @Before, @After, @Test or @AfterClass methods.

The test doesn't fail on start up as it seems it should. Can someone help me please?

I have JUnit 4.5

The method is failing in an immediate call to setUp() which is annotated as @before. Class def is :

public class myTests extends TestCase { 
like image 335
Lynden Shields Avatar asked Apr 09 '09 06:04

Lynden Shields


People also ask

Does @before run before each test?

Methods annotated with the @Before annotation are run before each test. This is useful when we want to execute some common code before running a test. Notice that we also added another method annotated with @After in order to clear the list after the execution of each test.

What is the difference between @before and @BeforeClass annotation?

The code marked @Before is executed before each test, while @BeforeClass runs once before the entire test fixture.

What is @BeforeClass in Java?

Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those of the current class, unless they are shadowed in the current class.

What is @BeforeClass in TestNG?

@BeforeClass annotated method will be run before the first test method in the current class is invoked. The following is a list of attributes supported by the @BeforeClass annotation: Attribute. Description.


2 Answers

do NOT extend TestCase AND use annotations at the same time!
If you need to create a test suite with annotations, use the RunWith annotation like:

@RunWith(Suite.class) @Suite.SuiteClasses({ MyTests.class, OtherTest.class }) public class AllTests {     // empty }   public class MyTests {  // no extends here     @BeforeClass     public static void setUpOnce() throws InterruptedException {         ...     @Test     ... 

(by convention: class names with uppercase letter)

like image 174
user85421 Avatar answered Sep 21 '22 07:09

user85421


the method must be static and not directly call fail (otherwise the other methods won't be executed).

The following class shows all the standard JUnit 4 method types:

public class Sample {      @BeforeClass     public static void beforeClass() {         System.out.println("@BeforeClass");     }      @Before     public void before() {         System.out.println("@Before");     }      @Test     public void test() {         System.out.println("@Test");     }      @After     public void after() {         System.out.println("@After");     }      @AfterClass     public static void afterClass() {         System.out.println("@AfterClass");     }  } 

and the ouput is (not surprisingly):

@BeforeClass @Before @Test @After @AfterClass 
like image 31
Vladimir Avatar answered Sep 21 '22 07:09

Vladimir