Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JUnit - Basic usage


May 15, 2021 jUnit


Table of contents


JUnit - Basic usage

Now we'll use simple examples to teach you how to use Junit step by step.

Some things to note about JUnit:

  • The test method must be @Test the test method
  • The test method must be decorated with public void and must not be with parameters
  • Unit tests are typically used to create a new test directory to store the test code, and only the test directory code needs to be removed when the test directory is deployed
  • The package for the test code should be consistent with the package structure under test
  • Each method in the test unit must be independently tested and there must be no dependencies between the methods
  • Test classes typically use Test as a suffix for class names
  • The test method makes test a prefix to the method name in general

The test failure description:

  • Failure: It is generally caused by inconsistent test results and expected results, indicating that a problem was found at this point in the test
  • Error: It is caused by a code exception, which can result from an error in the test code itself, or it can be a bug hidden in the test code

Some common annotations:

  • @Test: Retouching a normal method into a test method @Test (excepted s xx.class): xx.class represents an exception class, indicating that the test method throws this exception, the @Test (timeout s) that is considered normal when the test passes: whether the test method execution time is as expected
  • @BeforeClass: will be executed before all methods are executed, static method (global execution will only be performed once, and the first run)
  • @AfterClass: executed after all method executions, static methods (global execution only once, and the last run)
  • @Before: each test method is executed before it is run
  • @After: is executed once after each test method runs
  • @Ignore: The modified test method is ignored by the test runr
  • @RunWith: You can change the test runner org.junit.runner.runner
  • Parameters: Paramethy annotations

Create a class

  • In C: A class JUNIT_WORKSPACE MessageUtil is created under the .java to test.
/*
* This class prints the given message on console.
*/
public class MessageUtil {

   private String message;

   //Constructor
   //@param message to be printed
   public MessageUtil(String message){
      this.message = message;
   }

   // prints the message
   public String printMessage(){
      System.out.println(message);
      return message;
   }   
}  

Create a Test Case class

  • Create a test class .java TestJunit.
  • Add a method called testPrintMessage() to the test class.
  • Add an Annotaion @Test.
  • Perform test conditions and apply Junit's assertEquals API to check.

In C: C reate a JUNIT_WORKSPACE file named TestJunit under the .java path

import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestJunit {

   String message = "Hello World";  
   MessageUtil messageUtil = new MessageUtil(message);

   @Test
   public void testPrintMessage() {
      assertEquals(message,messageUtil.printMessage());
   }
}

Create a Test Runner class

  • Create a TestRunner class
  • Run the test case for the above test class using JUnit's runClasses method for the JUnitCore class
  • Get the results of a test case that runs in Result Object
  • Gets the result of a failure in Result Object's getFailures() method
  • Get the successful results in Result object's wasSuccessful() method

In C: T he test case JUNIT_WORKSPACE a class with the file name TestRunner .java the testrunner under the path

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(TestJunit.class);
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      System.out.println(result.wasSuccessful());
   }
}   

The MessageUtil, Test case, and Test Runner classes are compiled with javac.

C:\JUNIT_WORKSPACE>javac MessageUtil.java TestJunit.java TestRunner.java

Now run Test Runner, which can run test cases defined in the provided Test Case class.

C:\JUNIT_WORKSPACE>java TestRunner

Check the results of the run

Hello World
true

Now update C: T estJunit JUNIT_WORKSPACE path and failed detection. C hange the message string.

import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestJunit {

   String message = "Hello World";  
   MessageUtil messageUtil = new MessageUtil(message);

   @Test
   public void testPrintMessage() {
      message = "New Word";
      assertEquals(message,messageUtil.printMessage());
   }
}

Let's keep the other classes the same and try to run the same Test Runner again

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(TestJunit.class);
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      System.out.println(result.wasSuccessful());
   }
}

Now run Test Runner, which is about to run a test case that is available in the Test Case class

C:\JUNIT_WORKSPACE>java TestRunner

Check the results of the run

Hello World
testPrintMessage(TestJunit): expected:<[New Wor]d> but was:<[Hello Worl]d>
false