Reputation: 522
When I run my @Tests manualy, one by one - everything is fine. But when I run them all together - I`ve got an error. So how can I run @BeforeClass before each @Test. I cant use @Before becorse in my @BeforeClass I do some work in testing class constructor.
Testing class constructor:
public HttpUtils() {
this.httpClient = HttpClients.createDefault();
}
Before class:
@BeforeClass
public static void init() throws IOException {
mockStatic(HttpClients.class);
final CloseableHttpClient closeableHttpClient = createMock(CloseableHttpClient.class);
when(HttpClients.createDefault()).thenReturn(closeableHttpClient);
}
If I run all test. On second test Ive got HttpClient not like mock, but like real object, and lately have error coz of it.
Upvotes: 0
Views: 2159
Reputation: 13
If you want to execute any method before each test class you should go for @Before annotation. By using @BeforeClass annotation you only call that method once in the test class.
Upvotes: 0
Reputation: 58822
Use @Before instead of @BeforeClass
to execute before every test
@Before
public static void init() throws IOException {
with @Before causes that method to be run before the Test method. The @Before methods of superclasses will be run before those of the current class.
Upvotes: 4