Java junit before method

JUnit 5 @BeforeAll and @AfterAll Example

This page will walk through JUnit 5 @BeforeAll and @AfterAll example. @BeforeAll and @AfterAll annotations are analogous to @BeforeClass and @AfterClass respectively of JUnit 4. @BeforeAll and @AfterAll methods execute before and after respectively of all @Test , @RepeatedTest , @ParameterizedTest , or @TestFactory methods in current class. It means @BeforeAll method executes before all test methods in current class and @AfterAll method executes after all test methods in current class. @BeforeAll and @AfterAll methods must be static and must not return a value. But in interface we can create non-static @BeforeAll and @AfterAll methods using @TestInstance annotation and those methods will be default methods. Now find the complete example for @BeforeAll and @AfterAll annotations step by step.

Contents

Technologies Used

Find the technologies being used in our example.
1. Java 9
2. JUnit 5
3. Gradle 4.3.1
4. Eclipse Oxygen.3a

Gradle Build File

apply plugin: ‘java’ apply plugin: ‘eclipse’ archivesBaseName = ‘concretepage’ version = ‘1’ repositories < mavenCentral() >dependencies

Create Method using @BeforeAll and @AfterAll

@BeforeAll and @AfterAll methods must be static and must not return a value. Here we will create a test class using @Test annotation. Find the example.
UsingTestAnnotation.java

package com.concretepage; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class UsingTestAnnotation < @BeforeAll static void initAll() < System.out.println("---Inside initAll---"); >@Test @DisplayName("Multiplication Test") void multiplyTest() < System.out.println("---Inside multiplyTest---"); Utility utility = new Utility(); assertEquals(30, utility.multiplyNumbers(5, 6), "6 * 5 will be 30"); >@AfterAll static void tearDownAll() < System.out.println("---Inside tearDownAll---"); >>
---Inside initAll--- ---Inside multiplyTest--- ---Inside tearDownAll---
package com.concretepage; import java.util.Arrays; import java.util.List; public class Utility < List&ltString&gt allUsers = Arrays.asList("Mahesh", "Ram", "Krishn", "Arjun", "Bheem"); public long multiplyNumbers(int num1, int num2) < return num1 * num2; >public long addNumbers(int num1, int num2) < return num1 + num2; >public boolean isUserAvailable(String userName) < return allUsers.contains(userName); >>

@BeforeAll and @AfterAll with @BeforeEach and @AfterEach

@BeforeEach method executes after @BeforeAll method. @AfterEach method executes before @AfterAll method. @BeforeAll method executes before all test methods whereas @BeforeEach method executes before each test method in the current class. @AfterAll method executes after all test methods whereas @AfterEach method executes after each test method in the current class. Find the example of @BeforeAll and @AfterAll methods with @BeforeEach and @AfterEach methods.
WithBeforeEachAfterEach.java

package com.concretepage; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInfo; public class WithBeforeEachAfterEach < @BeforeAll static void initAll() < System.out.println("---Inside initAll---"); >@BeforeEach void init(TestInfo testInfo) < System.out.println("Start. " + testInfo.getDisplayName()); >@Test @DisplayName("Multiplication Test") void multiplyTest() < System.out.println("---Inside multiplyTest---"); Utility utility = new Utility(); assertEquals(30, utility.multiplyNumbers(5, 6), "6 * 5 will be 30"); >@AfterEach void tearDown(TestInfo testInfo) < System.out.println("Finished. " + testInfo.getDisplayName()); >@AfterAll static void tearDownAll() < System.out.println("---Inside tearDownAll---"); >>
---Inside initAll--- Start. Multiplication Test ---Inside multiplyTest--- Finished. Multiplication Test ---Inside tearDownAll---

@BeforeAll and @AfterAll with @Test, @RepeatedTest, @ParameterizedTest and @TestFactory

Here we will use @BeforeAll and @AfterAll methods with @Test , @RepeatedTest , @ParameterizedTest , and @TestFactory methods. We will observe that @BeforeAll method will execute before all test methods and @AfterAll method will execute after all test methods.
MyJUnitTests.java

package com.concretepage; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.stream.Stream; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.TestInfo; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; public class MyJUnitTests < @BeforeAll static void initAll() < System.out.println("---Inside initAll---"); >@BeforeEach void init(TestInfo testInfo) < System.out.println("Start. " + testInfo.getDisplayName()); >@Test @DisplayName("Multiplication Test") void multiplyTest() < System.out.println("---Inside multiplyTest---"); Utility utility = new Utility(); assertEquals(30, utility.multiplyNumbers(5, 6), "6 * 5 will be 30"); >@RepeatedTest(3) void repeatedMultiplicationTest() < System.out.println("---Inside repeatedMultiplicationTest---"); Utility utility = new Utility(); assertEquals(30, utility.multiplyNumbers(5, 6), "6 * 5 will be 30"); >@ParameterizedTest @ValueSource(strings = < "Krishn", "Arjun", "Bheem" >) void checkUserAvailabilityTest(String userName) < System.out.println("---Inside checkUserAvailabilityTest---"); Utility utility = new Utility(); assertTrue(utility.isUserAvailable(userName), "User will exist"); >@TestFactory @DisplayName("User Availability Test with @TestFactory") Stream<DynamicTest> userAvailabilityDynamicTest() < System.out.println("---Inside userAvailabilityDynamicTest---"); Utility utility = new Utility(); return Stream.of("Krishn", "Arjun", "Bheem") .map(userName -&gt DynamicTest.dynamicTest("Test " + userName, () -&gt < System.out.println(userName); assertTrue(utility.isUserAvailable(userName), "User will exist"); >)); > @AfterEach void tearDown(TestInfo testInfo) < System.out.println("Finished. " + testInfo.getDisplayName()); >@AfterAll static void tearDownAll() < System.out.println("---Inside tearDownAll---"); >>
---Inside initAll--- Start. Multiplication Test ---Inside multiplyTest--- Finished. Multiplication Test Start. [1] Krishn ---Inside checkUserAvailabilityTest--- Finished. [1] Krishn Start. [2] Arjun ---Inside checkUserAvailabilityTest--- Finished. [2] Arjun Start. [3] Bheem ---Inside checkUserAvailabilityTest--- Finished. [3] Bheem Start. User Availability Test with @TestFactory ---Inside userAvailabilityDynamicTest--- Krishn Arjun Bheem Finished. User Availability Test with @TestFactory Start. repetition 1 of 3 ---Inside repeatedMultiplicationTest--- Finished. repetition 1 of 3 Start. repetition 2 of 3 ---Inside repeatedMultiplicationTest--- Finished. repetition 2 of 3 Start. repetition 3 of 3 ---Inside repeatedMultiplicationTest--- Finished. repetition 3 of 3 ---Inside tearDownAll---

@BeforeAll and @AfterAll with Interface Static Methods

We can use @BeforeAll and @AfterAll with interface static methods. We will create here a lifecycle logger using interface. To use our logger, we will create a class implementing the interface and then write test methods. Find the example.
LifecycleLogger.java

package com.concretepage; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.TestInfo; public interface LifecycleLogger < @BeforeAll static void initAll() < System.out.println("---Inside initAll---"); >@BeforeEach default void init(TestInfo testInfo) < System.out.println("Start. " + testInfo.getDisplayName()); >@AfterEach default void tearDown(TestInfo testInfo) < System.out.println("Finished. " + testInfo.getDisplayName()); >@AfterAll static void tearDownAll() < System.out.println("---Inside tearDownAll---"); >>
package com.concretepage; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class MyMultiplicationTest implements LifecycleLogger < @Test @DisplayName("Multiplication Test") void multiplyTest() < System.out.println("---Inside multiplyTest---"); Utility utility = new Utility(); assertEquals(30, utility.multiplyNumbers(5, 6), "6 * 5 will be 30"); >>
---Inside initAll--- Start. Multiplication Test ---Inside multiplyTest--- Finished. Multiplication Test ---Inside tearDownAll---

@BeforeAll and @AfterAll with Interface Non-Static Methods (Default Methods) using @TestInstance

We can use @BeforeAll and @AfterAll annotations with non-static methods (default methods) of interface using @TestInstance annotation. @TestInstance is a type-level annotation that is used to configure lifecycle of instances of annotated test class or test interface. @TestInstance works with following lifecycle mode.

Читайте также:  Java anonymous classes this

Lifecycle.PER_METHOD: A new test instance will be created for each test method or test factory method. PER_METHOD mode is the default mode when @TestInstance has not explicitly declared on test class or test interface.

Lifecycle.PER_CLASS: A new test instance will be created once per test class. The advantage of PER_CLASS mode is that we can use non-static @BeforeAll and @AfterAll methods.

In our example we will create a lifecycle logger interface annotated with @TestInstance with mode Lifecycle.PER_CLASS and we will create non-static @BeforeAll and @AfterAll methods as interface default methods. Find the example.
LifecycleLoggerWithTestInstance.java

package com.concretepage; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.TestInfo; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance.Lifecycle; @TestInstance(Lifecycle.PER_CLASS) public interface LifecycleLoggerWithTestInstance < @BeforeAll default void initAll() < System.out.println("---Inside initAll---"); >@BeforeEach default void init(TestInfo testInfo) < System.out.println("Start. " + testInfo.getDisplayName()); >@AfterEach default void tearDown(TestInfo testInfo) < System.out.println("Finished. " + testInfo.getDisplayName()); >@AfterAll default void tearDownAll() < System.out.println("---Inside tearDownAll---"); >>
package com.concretepage; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class MyAdditionTest implements LifecycleLoggerWithTestInstance < @Test @DisplayName("Add Test") void addTest() < System.out.println("---Inside addTest---"); Utility utility = new Utility(); assertEquals(11, utility.addNumbers(5, 6), "6 + 5 will be 11"); >>
---Inside initAll--- Start. Add Test ---Inside addTest--- Finished. Add Test ---Inside tearDownAll---

To run the example, download the source code and import into Eclipse Oxygen IDE. To run a test class, right click on the test class -&gt Run As -&gt JUnit Test. We will get output in console. If we are using JUnit 4 environment to run JUnit 5 examples then we need to annotate our test class with @RunWith(JUnitPlatform.class) annotation. Eclipse Oxygen.1a onwards provides JUnit 5 environment. In our example, we are using Eclipse Oxygen.3a to run test classes.

Источник

Туториал по JUnit 5 — Аннотация @BeforeAll

Аннотация JUnit 5 помечает @BeforeAll метод, являющийся методом жизненного цикла JUnit 5 теста. @BeforeAll — это замена аннотации @BeforeClass в JUnit 4.

Это продолжение туториала по JUnit 5. Введение опубликовано здесь.

1. Аннотация @BeforeAll

Аннотация @BoforeAll используются для указания о том, что аннотированный метод должен быть выполнен перед всеми @Test , @RepeatedTest , @ParameterizedTest и @TestFactory методами в текущем классе.

По умолчанию тестовые методы будут выполняться в том же потоке, что и аннотированный @BeforeAll метод.

@BeforeAll public static void init()
  • Или мы можем применить эту аннотацию к default методам интерфейса, если тестовый интерфейс или тестовый класс аннотированы с помощью @TestInstance(Lifecycle.PER_CLASS).
@TestInstance(Lifecycle.PER_CLASS) interface TestLifecycleLogger < @BeforeAll default void beforeAllTests() < // >>

Если этого не сделать, JUnit выдаст ошибку времени выполнения типа JUnitException .

org.junit.platform.commons.JUnitException: @BeforeAll method 'public void com.howtodoinjava.junit5.examples. JUnit5AnnotationsExample.init()' must be static. at org.junit.jupiter.engine.descriptor. LifecycleMethodUtils.assertStatic(LifecycleMethodUtils.java:66)

@BeforeAll в родительском и дочернем классах

@BeforeAll методы наследуются от родительских классов (или интерфейсов), если они не скрыты или не переопределены.

Кроме того, @BeforeAll методы из родительских классов (или интерфейсов) будут выполняться перед @BeforeAll методами в дочерних классах.

2. Пример аннотации @BeforeAll

Рассмотрим пример. Мы написали тест для класса Calculator и его метода add() .

Выполним тест 5 раз с помощью аннотации @RepeatedTest . Аннотация @Repeated вызовет запуск теста add пять раз. Но аннотированный @BeforeAll метод нужно вызывать только один раз.

package com.howtodoinjava.junit5.examples; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.RepetitionInfo; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInfo; public class BeforeAllTest < @DisplayName("Add operation test") @RepeatedTest(5) void addNumber(TestInfo testInfo, RepetitionInfo repetitionInfo) < System.out.println("Running test ->" + repetitionInfo.getCurrentRepetition()); Assertions.assertEquals(2, Calculator.add(1, 1), "1 + 1 should equal 2"); > @BeforeAll public static void init() < System.out.println("Before All init() method called"); >>
package com.howtodoinjava.junit5.examples; public class Calculator < public static int add(int a, int b) < return a + b; >>

Теперь выполните тест, и вы увидите на консоли вывод:

Before All init() method called Running test -> 1 Running test -> 2 Running test -> 3 Running test -> 4 Running test -> 5

Понятно, что аннотированный @BeforeAll метод init() вызывается только один раз.

Источник

Оцените статью