The below examples are specific to Junit4
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Basic Test case which Fails
import static org.junit.Assert.*;
import org.junit.Test;
public class Junit1 {
@Test
public void basicTestFail()
{
fail("The Test Failed");
}
}
There is No pass method in JUnit like fail method.
As long as the test doesn’t throw an exception, it passes, unless your @Test annotation specifies an expected exception. I suppose a pass() could throw a special exception that JUnit always interprets as passing, so as to short circuit the test, but that would go against the usual design of tests (i.e. assume success and only fail if an assertion fails) and, if people got the idea that it was preferable to use pass(), it would significantly slow down a large suite of passing tests (due to the overhead of exception creation). Failing tests should not be the norm, so it’s not a big deal if they have that overhead.
Tests to verify the correctness of a program’s behavior.Verifying the correctness of a program’s behaviour by inspecting the content of output statements.Its looking manually for abnormal output.
Example1
StringHelper.java
package com.mugil.junit.tutor;
public class StringHelper
{
public static void main(String[] args)
{
StringHelper objStringHelper = new StringHelper();
System.out.println(objStringHelper.wordATrimmer("PABBB"));
}
public String wordATrimmer(String pWord)
{
String pTempString = pWord.substring(0, 2);
if(pTempString.contains("A")) return pWord.replace("A", "");
pTempString = pWord.substring(2);
return pWord;
}
}
StringHelperTest.java
package com.mugil.junit.tutor;
import static org.junit.Assert.*;
import org.junit.Test;
public class StringHelperTest
{
@Test
public void test()
{
StringHelper objStringHelper = new StringHelper();
String expectedOP = "MMG";
String actOP = objStringHelper.wordATrimmer("AAMMG");
assertEquals(expectedOP, actOP);
}
}
@Test comes from org.junit.Test Package
assertEquals – helps in performing String, Boolean and Other Comparison.
As you have seen above StringHelper.java is the actual file that has the function.StringHelperTest.java is the file in which test of the functionality is to be carried out. This java file is created in a new source folder test. By using the assert equals method we are comparing whether the expected Op and actual OP are the same.
@Test annotation at the top of the test method tells the JVM to run this method
assertTrue(boolean)
assertTrue(boolean true) will return true
assertFalse(boolean false) will return true
To make sure the code is behaving the right way we can use asserttrue() and assertfalse() methods.
StringHelper.java
package com.mugil.junit.tutor;
public class StringHelper
{
public boolean areFirstTwoandLastTwoStringsSame(String Name)
{
if(Name.length() < 2) return false;
if(Name.length() == 2) return true;
String firstChars = Name.substring(0, 2);
String lastChars = Name.substring(Name.length()-2);
if(firstChars == lastChars)
return true;
else
return false;
}
}
StringHelperTest.java
package com.mugil.junit.tutor;
import static org.junit.Assert.*;
import org.junit.Test;
public class StringHelperTest
{
@Test
public void test()
{
StringHelper objStringHelper = new StringHelper();
assertTrue(objStringHelper.areFirstTwoandLastTwoStringsSame("AB"));
assertFalse(objStringHelper.areFirstTwoandLastTwoStringsSame("ABAB"));
}
}
The above code checks whether First and Last two characters are the same and return true or false based on input to method areFirstTwoandLastTwoStringsSame.
The before and after methods are used to run a case before and after the actual test started.
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class StringHelperTest
{
@Before
public void before()
{
System.out.println("Before Test");
}
@After
public void after()
{
System.out.println("After Test");
}
@Test
public void test1()
{
System.out.println("Test1");
}
@Test
public void test2()
{
System.out.println("Test2");
}
}
The output of the above code would be
Output
Before Test
Test1
After Test
Before Test
Test2
After Test
You can also run a test before and after the class run as below
import org.junit.AfterClass;
import org.junit.BeforeClass;
@BeforeClass
public static void beforeClass()
{
System.out.println("Before Test");
}
@AfterClass
public static void afterClass()
{
System.out.println("After Test");
}
@Test
public void test1()
{
StringHelper objStringHelper = new StringHelper();
System.out.println("During Test");
}
Output
Before Test
During Test
After Test
BeforeClass and AfterClass is run only once when object for class is created.Note the static before beforeClass and afterClass methods.
Typical Example for Using before and after methods where object for class created in before method and object assigned null in after method.
public class StringHelperTest
{
StringHelper objStringHelper1;
@Before
public void before()
{
objStringHelper1 = new StringHelper();
}
@After
public void afterClass()
{
objStringHelper1 = null;
}
@Test
public void test1()
{
System.out.println("Test Cases");
}
}
import static org.junit.Assert.*;
import org.junit.Test;
public class StringHelperTest
{
@Test
public void test1()
{
int[] arrNums1 = {5,4,3,2,1};
Arrays.sort(arrNums1);
int[] arrNums2 = {1,2,3,4,5};
assertArrayEquals(arrNums1, arrNums2);
}
}
assertArrayEquals method comes from org.junit.Assert Package
We are going to look for the expected exception is getting thrown or not
import java.util.Arrays;
import org.junit.Test;
public class StringHelperTest
{
@Test(expected=NullPointerException.class)
public void test1()
{
int[] arrNums1 = null;
Arrays.sort(arrNums1);
}
}
– How Much time is taken by code block to execute the code.
In the below code we are going to estimate actual time taken by the code block to execute
public class StringHelperTest
{
@Test(timeout=100)
public void test1()
{
for (int i = 0; i < 1000000; i++)
{
int[] arrNumbs = {i-1, i, i+1};
Arrays.sort(arrNumbs);
}
}
}
timeout=100 – is going to tell the code should complete the execution within 100 milliseconds.If it fails I should optimize the code by performance tweaks to boost codes performance.