Examples of JUnit 5 – @BeforeAll
Example 1 :
Let us take an example, we have written a test for the String Handler class to check whether the given string is empty or not
Java
//Class to test String trim method public class StringHandler { //To Perform operations on the input string public static String manipulateString(String input) { // Trim the input string return input.trim(); } } |
Now we write a test case for it to check whether the given String is empty or not
In the above code, I take a string value and remove white spaces by using the trim() string function then return that string value.
Java
// Testing file to test the manipulate // String Class import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; //Testing Class public class StringTest { private static String testString; // This method is executed once before any of the test methods in the class @BeforeAll static void init() { // Manipulate the input string and store it in the testString variable testString = StringHandler.manipulateString( " Hello, GFG! " ); // Print the manipulated test string System.out.println( "Test String: '" + testString + "'" ); } // Test whether the manipulated string is not empty @Test void testStringNotEmpty() { // Print a message indicating that the testStringNotEmpty test is being run System.out.println( "Running testStringNotEmpty..." ); // Assert that the manipulated test string is not empty assertTrue(!testString.isEmpty(), "The string should not be empty" ); } // Test whether an empty string is indeed empty @Test void testStringEmpty() { // Print a message indicating that the testStringEmpty test is being run System.out.println( "Running testStringEmpty..." ); // Create an empty string for the test and assert that it is empty String emptyString = "" ; assertTrue(emptyString.isEmpty(), "The string should be empty" ); } } |
Output:
Test String: 'Hello, GFG!'
Running testStringNotEmpty...
Running testStringEmpty...
Explanation of the above Program:
In the above code, I created one static method for @BeforeAll,
- this static method is mandatory for work with @BeforeAll annotation.
- The @BeforeAll is checked first before all test cases are run.
- Only once after that, I create the testStringNotEmpty() function testing if the string is empty or not then return the Running testStringNotEmpty as output.
- After that, I created another testStringEmpty() method for if the string is empty it will return Running testStringEmpty() as an output you can observe the below output.
Example 2 :
Simple Java class to check whether a person is eligible to vote or not based on their age.
Java
//Class to determine VotingEligibility public class VotingEligibility { //Biz-logic for VotingEligibility method public static String checkEligibility( int age) { // checking the age of the person if (age >= 18 ) { // if age is elibible then this block executed System.out.println( "Person is eligible to vote." ); return "Eligible to vote" ; } else { // if age is not eligible then this code is executed System.out.println( "Person is not eligible to vote." ); return "Not eligible to vote" ; } } } |
- In the above code, I try to check whether the person is eligible to Vote or not based on the above condition (age >=18).
- If the person’s age is less than 18 then the else block is executed.
- If the age is greater than 18 then the if block code is executed and print the Appropriate Output.
Java
//TESTING file to test the VotingEligibility Class import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; //Testing class for VotingEligibility public class VotingEligibilityTest { // Variables to store the results of the voting eligibility checks private static String resultEligible; private static String resultNotEligible; // Method to perform setup operations before any of the test methods in the class @BeforeAll static void init() { // Check the eligibility for a person with age 25 and store the result resultEligible = VotingEligibility.checkEligibility( 25 ); // Check the eligibility for a person with age 16 and store the result resultNotEligible = VotingEligibility.checkEligibility( 16 ); } // Test method to check if a person is eligible to vote @Test void testEligibleToVote() { // Print a message indicating that the testEligibleToVote test is being run System.out.println( "Running testEligibleToVote..." ); // Assert that a person with age 25 is eligible to vote assertEquals( "Eligible to vote" , resultEligible, "Should be eligible to vote" ); } // Test method to check if a person is not eligible to vote @Test void testNotEligibleToVote() { // Print a message indicating that the testNotEligibleToVote test is being run System.out.println( "Running testNotEligibleToVote..." ); // Assert that a person with age 16 is not eligible to vote assertEquals( "Not eligible to vote" , resultNotEligible, "Should not be eligible to vote" ); } } |
Output:
Person is eligible to vote.
Person is not eligible to vote.
Running testEligibleToVote...
Running testNotEligibleToVote...
Explanation of the above Program:
In the above code, I created three methods for testing purposes the static method is for @BeforeAll annotation.
- The testEligibleToVote() method is used to check whether a person is greater than 18 or not if it is true then this returns output as Running testEligibleToVote.
- The other method is used for testNotEligibleToVote() is used for printing messages if the given age is not eligible to vote.
JUnit 5 @BeforeAll Annotation with Examples
JUnit 5 is the unit testing framework in Java, and it is not a tool rather the JUnit 5 framework contains a set of tools and libraries that are used to write and run test cases. The Junit 5 Framework was introduced in 2017, This was developed by Kent Beck, Erich Gamma, David Saff, and Kris Vasudevan. Its Operating System is cross-platform and this framework is written in Java. This Framework makes it easier to extend the framework, and we can able integrate with other testing frameworks also, In JUnit 5 several new Annotations are introduced those are @BeforeAll, @AfterAll, @BeforeEach, @AfterEach, @TestFactory, @DisplayName, @Disabled, @Nested, @Tag. Among these annotations, I give Information about @BeforeAll annotation.
Contact Us