Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit: @Before only for some test methods?

I have some common set up code that I've factored out to a method marked with @Before. However, it is not necessary for all this code to run for every single test. Is there a way to mark it so the @Before method only runs before certain tests?

like image 695
Nick Heiner Avatar asked Oct 10 '09 16:10

Nick Heiner


People also ask

Does @before run before each test?

Methods annotated with the @Before annotation are run before each test. This is useful when we want to execute some common code before running a test. Notice that we also added another method annotated with @After in order to clear the list after the execution of each test.

Can we have two @before in JUnit?

The JUnit Team therefore recommends that developers declare at most one @BeforeEach method and at most one @AfterEach method per test class or test interface unless there are no dependencies between the @BeforeEach methods or between the @AfterEach methods.

What is the purpose of @before annotation in JUnit?

The @Before annotation is used when different test cases share the same logic. The method with the @Before annotation always runs before the execution of each test case. This annotation is commonly used to develop necessary preconditions for each @Test method.

Does @after run after every test?

Create Test fixture before each test.  @Before - method that is run before every test case. setUp( ) is the traditional name.  @After - method that is run after every test case.


2 Answers

Just move out the tests that don't need the setup code into a separate test class. If you have some other code common to the tests that would be helpful to keep, move that out into a helper class.

like image 93
Kirschstein Avatar answered Oct 08 '22 04:10

Kirschstein


@Nested + @BeforeEach

Totally agree with the point of moving the related code to an inner class. So here what I have done.

  1. Create an inner class inside your test class
  2. Annotate the inner class with @Nested
  3. Move all the test methods you want to use in the inner class
  4. Write the init code inside the inner class and annotate it with @ForEach

Here is the code:

class Testing {      @Test     public void testextmethod1() {            System.out.println("test ext method 1");          }          @Nested     class TestNest{             @BeforeEach        public void init() {           System.out.println("Init");        }             @Test        public void testmethod1() {           System.out.println("This is method 1");        }             @Test        public void testmethod2() {           System.out.println("This is method 2");        }             @Test        public void testmethod3() {           System.out.println("This is method 3");        }               }       @Test      public void testextmethod2() {               System.out.println("test ext method 2");           }  } 

Here is the output

test ext method 1 test ext method 2 Init This is method 1 Init This is method 2 Init This is method 3 

Note: I am not sure if this is supported in Junit4. I am doing this in JUnit5

like image 26
Rohit Singh Avatar answered Oct 08 '22 06:10

Rohit Singh