Week 8 – Unit Testing (JUnit)

Unit Test 

Definition & Usefulness 

Unit Test- a piece of code that is written by a developer that executes a specific functionality in the code to be tested.

  • Unit Test targets a method or a class usually
  • Unit Test ensures that code works as intended
  • Unit Test helps you find bugs in a code
  • Using Unit Test you won’t have to manually write the tests each time
Test Organization
  • Typically Unit tests are created in a separate project or a separate source folder
  • Effect unit test are those which test every line of your code
J Unit
  • JUnit is a test framework which uses annotations to identify methods that specify a test
  • Consider the following function

public void multiplicationOfZeroShouldReturnZero(){

MyClass tester = new MyClass();

// comment –assertEquals(message, expectedoutput, actualoutput)

//comment — assertEquals doesn’t have to contain a message

assertEquals(“10×0 = 0”, 0, tester.multiply(10,0));

}


Test Names & Content

  • It’s a good practice to name the methods that what they are testing, for instance: multiplicationOfZeroShouldReturnZero()

A Test case may have more than one assertEquals to test different cases with messages to distinguish the those cases

Assert Statements
  • Fails(String) – let the method fail might be used to check if a certain part of the code is not reached
  • assertTrue([message], boolean condition) – checks if the boolean condition is true
  • assertFalse([message], boolean condition) – checks if the boolean condition is false
  • assertEquals([String message], expected, actual) – matches the expected the output with the actual output
  • assertEquals([String message], expected, actual, tolerance) -the amount of different (usually in numbers like 0.000001 that you are okay with tolerating and calling them the same)
  • assertNull([message], object) – checks if the object is null
  • assertNotNull([message], object) – checks if the object is not null
  • assertNotSame([String], expected, actual)- checks that both variables refer to different object

Single-Outcome Assertions- Fail

Stated-Outcome Assertions – assertNotNull(object) OR assertNotNull(message, object), assertTrue(boolean expression) OR assertTrue(message, boolean expression)

Equality Assertions – assertEquals(expected, actual) OR assertEquals(message, expected, actual)

Fuzzy Equality Assertions – assertEquals(message, expected, actual, tolerance)


Possible results

Pass- test produced the expected output

Fail- test ran but produced an incorrect output

Error- test ran but produced an incorrect behaviour, such as threw an unexpected exception


Testing code with Exceptions

public void testIndexOutOfBoundsException(){
  ArrayList emptyList = new ArrayList();
  Object o = emptyList.get(0);
}

public void testIndexOutOfBoundsException(){
  ArrayList emptylist = new ArrayList();
  try{
      Object o = emptyList.get(0);
      AssertFail("IndexOutOfBoundsException not thrown: 0")
  } catch (IndexOutOfBoundsException e) {}
}

Unit Testing Example

Consider the following class & its functions

public class StringHelper{
  
  //removes "A" in the first two positions
  public String truncateA(String str){
     
     if (str.length() == 0){
       return str;
     }

     if (str.length() < 2){
       return str.repaceAll("A","");
     }

     String first2Chars = str.substring(0,2);
     String afterFirst2Chars = str.substring(2);
     
     return first2Chars.replaceAll("A","")+afterFirst2Chars; 
  }
}
public class StringHelperTest{
   
   @Test
   public void test(){
      StringHelper sh = new StringHelper();
      String actual = sh.truncateA("BACD");
      String expected = "BCD";
      assertEquals(actual, expected);
   }
   
   @Test
   public void testScenario2(){
     StringHelper sh = new StringHelper();
     String actual = sh.truncateA("BCDA");
     String expected = "BCDA"
     assertEquals(actual, expected);
   }

Leave a comment