JUnit

https://github.com/rdayala/spring-master-class/tree/master/00-framework-tool-introductions/03.JUnit-Introduction-In-5-Steps

Testing is typically done to make sure that the functionality of the application is as expected. This is to check whether the application is behaving as expected.

If you actually depend on deploying the application and testing it after deployment, then what would happen is, you would be testing big chunks of functionality. – this is kind of System Testing.

Unit Testing is different, where you would be testing a specific method or a class.

Unit Testing is writing individual methods for methods, group of methods or for a class. Most of the cases, they are addressed to a specific method.

JUnit is a framework which helps us to do Unit Testing. Unit testing is testing a specific method or a group of methods. One of the advantage of framework is, the tests are automated.

JUnit test cases can be run in Continuous integration.

Unit tests should be in a different folder compared to Production code. It’s not a good practice to keep both source code and unit tests in same folder.

src/main/java – where all Java sources will be written.
src/test/java – unit tests will be written here

Right click on test folder -> New -> selection wizard -> Create a JUnit Test case –> Add JUnit library to the build path.

Typically we follow this convention for Test code :

source file name : MyMath.java
unit test file name : MyMathTest.java

@Test – this annotation is used to signify something is a unit test. This method contains a unit test.

Note: Absence of failure is success in JUnit. Even if we don’t write any unit test, if we just run the code, it succeeds.

@Test
public void sum_with3numbers(){
    MyMath myMath = new MyMath();
    int result = myMath.sum(new int[]{1, 2, 3});
    // assertEquals(expected, actual)
    assertEquals(6, result);
}

@Test
public void sum_with1number(){
    MyMath myMath = new MyMath();
    int result = myMath.sum(new int[]{3});
    // assertEquals(expected, actual)
    assertEquals(3, result);
}

Other Assert methods :

  • assertEquals(expected, actual)
  • assertTrue(condition)
  • assertFalse(condition)
  • assertNotNull(Object)
  • assertNull(object)
  • assertArrayEquals(expeceds, actuals)

Important Annotations :

@Before – method annotated with this annotation is called before each unit test @Test method is called.

@After – method annotated with this annotation is called after each unit test @Test method is run.

@BeforeClass – method annotated with this is annotation is called once before running any unit tests @Test methods. This should be a static method.

@AfterClass – method annotated with this annotation is called after all unit tests are run.

@Before
public void before() {
    System.out.println("Before");
}

@After
public void after() {
    System.out.println("After");
}

@BeforeClass
public static void beforeClass() {
 System.out.println("Before class");
}

@AfterClass
public static void afterClass() {
 System.out.println("After class");
}