JUnit And Mockito

JUnit

Testing Framework for Java

  • JUnit is an open-source testing framework for java Developers. The Java Developer can create a test case and test his own code.
  • It is also one of the testing frameworks.
  • To perform unit testing, the developer thus needs to create test cases. The unit test case is a code that checks that the program logic works correctly.
  • The org.jUnit package have interfaces and classes for testing such as Assert, Test, Before, After.

Types of unit testing

There are the following ways for unit testing:

  1. Manual testing
  2. Automated testing.
1. Manual Testing
  • It is also time-consuming and less reliable.
  • thus, It is executing the test cases manually without any tool support.
2. Automated Testing
  • therefore, It is fast and reliable.
  • It thus executes the test cases manually with tool support.

Annotations for JUnit testing

The Junit framework is annotation based, so these are the annotations that can be used while writing the test cases.

@Test:

this annotation specifies that the method is the test method.

@Test(timeout=1000):

this annotation specifies that the method will be failed if it takes longer than 1000 milliseconds (1 second).

@BeforeClass:

this annotation specifies that the method will be invoked only once, before starting all the tests.

@Before:

this annotation specifies that the method will be invoke before each test.

@After:

this annotation specifies that the method will also be invoke after each test.

@AfterClass:

this annotation specifies that the method will be only called once, after finishing all the tests.

Assert class

  • The org.junit.Assert class provides methods/functions to assert the program logic.
Methods of Assert class

The methods of Assert class are as follows:

  • void assertEquals(boolean expected, boolean actual): checks whether two primitives/objects are equal. It is overloaded.
  • void assertTrue(boolean condition): checks that a condition is true.
  • and, void assertFalse(boolean condition): checks that a condition is false.
  • void assertNull(Object obj): checks that object is null.
  • void assertNotNull(Object obj): checks that object is not null.
Find the maximum number for an array.
Write the test case

to run this example, right click on TestLogic class -> Run As -> 1Junit Test.

Output: Assertion Error

MOCKITO

Mockito is a java based mocking/ testing framework, used with other testing frameworks such as JUnit. therefore, It uses the Java Reflection API and allows to create objects of service. A mock object returns useless data to avoid external dependencies. hence, It makes more simple the development of tests by mocking dependencies and apply the mocks in the code tests.

Mockito Mock Creation

@mock annotation is also utilize to create mock object.

or

mock() static method is in use to create mock object.

Example for Jmockito: 

(program reference:https://www.journaldev.com/21816/mockito-tutorial#:~:text=Mockito%20Tutorial%20Mockito%20is%20a%20java%20based%20mocking,returns%20a%20dummy%20data%20and%20avoids%20external%20dependencies. )

Conclusion:

However, Junit and Jmockito are testing frameworks in use of java developers. therefore, These are in use to test the logic and complexity of the program. so, Junit contains various types of interfaces and classes to test the logic cases.

hence, Jmockito is utilize with the conjunction of JUnit and other testing frameworks. so, This is useful to avoid the worst cases of the program, it is helpful to avoid the bugs in a java program, and the user can get error-free service.

written by: Rohit Damji

reviewed by: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *