HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / JUnit 5 / JUnit 5 @BeforeEach annotation example

JUnit 5 @BeforeEach annotation example

JUnit 5 @BeforeEach annotation is replacement of @Before annotation in JUnit 4. It is used to signal that the annotated method should be executed before each @Test method in the current class.

@BeforeEach Annotation Usage

Annotate a method with @BeforeEach as given below:

@BeforeEach
public void initEach(){
	System.out.println("Before Each initEach() method called");
}

@BeforeEach annotated method MUST NOT be a static method otherwise it will throw runtime error.

org.junit.platform.commons.JUnitException: @BeforeEach method 'public static void com.howtodoinjava.junit5.examples.JUnit5AnnotationsExample.initEach()' must not be static.
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.assertNonStatic(LifecycleMethodUtils.java:73)
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.lambda$findBeforeEachMethods$2(LifecycleMethodUtils.java:54)
	at java.util.ArrayList.forEach(ArrayList.java:1249)
	at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1080)
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findBeforeEachMethods(LifecycleMethodUtils.java:54)

@BeforeEach Annotation Example

Let’s take an example. I have used one Calculator class and added one add method. I will test it 5 times using @RepeatedTest annotation. This annotation will cause the add test to run 5 times. For every run of test method, @BeforeEach annotated method should also run every time.

package com.howtodoinjava.junit5.examples;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
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;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
public class BeforeEachTest {

	@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("BeforeAll init() method called");
	}
	
	@BeforeEach
	public void initEach(){
		System.out.println("BeforeEach initEach() method called");
	}
}

Where Calculator class is:

package com.howtodoinjava.junit5.examples;

public class Calculator 
{
	public int add(int a, int b) {
		return a + b;
	}
}

Now execute the test and you will see below console output:

BeforeAll init() method called
BeforeEach initEach() method called

BeforeEach initEach() method called
Running test -> 1

BeforeEach initEach() method called
Running test -> 2

BeforeEach initEach() method called
Running test -> 3

BeforeEach initEach() method called
Running test -> 4

BeforeEach initEach() method called
Running test -> 5

Clearly, @BeforeEach annotated initEach() method is called once per test method invocation.

Happy Learning !!

Sourcecode Download

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

JUnit 5 Tutorial

  • JUnit 5 – Introduction
  • JUnit 5 – Test LifeCycle
  • JUnit 5 – @BeforeAll
  • JUnit 5 – @BeforeEach
  • JUnit 5 – @AfterEach
  • JUnit 5 – @AfterAll
  • JUnit 5 – @RepeatedTest
  • JUnit 5 – @Disabled
  • JUnit 5 – @Tag
  • JUnit 5 – Expected Exception
  • JUnit 5 – Assertions Examples
  • JUnit 5 – Assumptions
  • JUnit 5 – Test Suites
  • JUnit 5 – Gradle Dependency
  • JUnit 5 – Maven Dependency
  • JUnit 5 – Execute Test in Eclipse
  • JUnit 5 – Eclipse Test Templates
  • JUnit 5 vs JUnit 4

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)