junit test case for help functionality code example

Example 1: junit testcase run in order

@TestMethodOrder(OrderAnnotation.class)
public class OrderAnnotationUnitTest {
    private static StringBuilder output = new StringBuilder("");
     
    @Test
    @Order(1)    
    public void firstTest() {
        output.append("a");
    }
     
    @Test
    @Order(2)    
    public void secondTest() {
        output.append("b");
    }
  
    @Test
    @Order(3)    
    public void thirdTest() {
        output.append("c");
    }
  
    @AfterAll
    public static void assertOutput() {
        assertEquals(output.toString(), "abc");
    }
}

Example 2: how to write a junit test case in java

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class MyTests {

    @Test
    public void multiplicationOfZeroIntegersShouldReturnZero() {
        MyClass tester = new MyClass(); // MyClass is tested

        // assert statements
        assertEquals(0, tester.multiply(10, 0), "10 x 0 must be 0");
        assertEquals(0, tester.multiply(0, 10), "0 x 10 must be 0");
        assertEquals(0, tester.multiply(0, 0), "0 x 0 must be 0");
    }
}

Tags:

Misc Example