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 {
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.
The code marked @Before is executed before each test, while @BeforeClass runs once before the entire test fixture.
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.
@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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With