1

I dont have to say very much. The following code sample prints "null", but i expect it to print "test". What is worg here?

import org.junit.BeforeClass;
import org.junit.jupiter.api.Test;

class AcceptanceTest {

    private String test;

    @BeforeClass
    public void setup() {
        test = "test";
    }

    @Test
    void test() {
        System.out.println(test);
    }

}
3
  • @BeforeClass signals that the code is to be executed before class instantiation which only works with static methods (as there is no class context to run them in). In such a method you can also not set a non-static value. You are probably not receiving error messages because you are running it with Jupiter JUnit. The BeforeClass annotation does not even get considered there so the code is never executed. Commented Jul 10, 2018 at 12:29
  • The solution is to use the Jupiter annotation @BeforeEach instead. Or change your version of JUnit as suggested by the answers below. Commented Jul 10, 2018 at 12:30
  • After reading a little bit more about the topic, the correct way to go is not just to make the variables and the setup method static, but to change BeforeClass to BeforeAll. Since i'm using Junit.Jupiter, the BeforeClass isn't quite compatible with the old Juinit.test. Commented Jul 10, 2018 at 12:54

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.