Everything about JUNIT

Avinash Dogiparthi
4 min readJun 15, 2023

--

  • Junit is a unit testing framework for Java
  • The framework is used by Java Developers to write and execute tests
  • Every time a new code is added, all the test cases have to be re-executed

Unit Testing

  • Unit testing refers to the testing of small chunk of codes
  • It helps in early identification of defects
  • The developers are tend to spend more time on reading the code
  • Successful code increases the confidence of the developers

JUnit Installation

Step 1 : Require JDK

Step 2 : Any IDE ( Eclipse preferred)

Step 3 : JUnit jar files

If you’re using maven as built tool then you need to add this dependency in pom.xml file

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>

Features of JUnit

  • JUnit is an open source network used to write and run tests
  • JUnit provides annotations to identify test methods
  • JUnit provides assertions to test expected results
  • JUnit provides test runners to run the tests
  • JUnit allows faster code writing, that increases the code’s quality
  • The tests in JUnit are automatically run, that provide instant feedback

JUnit Annotations

  • JUnit Annotations refers to the syntactic meta-data added to the Java source code for better structure and readability.
  • The biggest difference between JUnit 3 & JUnit4 is the introduction of JUnit Annotations.

Frequently used JUnit Annotations

@Test : Tells JUnit which public void method can be run as a test case.

@Before : To execute some statement before each test case.

@After : To execute some statement after each test case.

@Ignores : Used to ignore some statements during test execution.

@BeforeClass : Used to execute a statement before all the test cases

@AfterClass : Used to execute a statement after all the test cases.

@Test (timeout = 500) : Used to set some timeout while executing the test.

@Test (expected=illegalArgumentException.class) : Used to handle some exception during the test execution.

@BeforeEach : The setUp() method annotated with @BeforeEach is a setup method in JUnit 5. This method is executed before each test method in the test class.

Parameterized Test

  • Parameterized tests allow a developer to run the same test over and over again using different values

Output :

JUnit Testing in JAVA

Mockito

What is Mocking?

  • Mocking refers to the development of objects which are a mock or clone of real objects.
  • In the technique mock objects are used instead of real objects for testing.
  • Mock objects give a particular output for each particular input.
  • Mockito is the most popular framework used for mocking.

What is Mockito?

  • Mockito is a Java based framework used for unit testing of Java applications
  • This mocking framework helps in development of testable applications
  • Java Reflection API is used internally to generate mock objects
  • Mockito is used to simplify the test development by mocking external dependencies and using them in the code

When to use Mockito?

  • When a component is to be tested, but that component depends on some other component, which is under development.
  • When the real component perform slowly, then mock objects are used to perform testing.
  • When there are any concerns with the infrastructure that makes the testing impossible

What is a Stub?

  • Stubs are objects holding predefined data and uses the data to give responses during the tests
  • It can be refereed to as an object that looks like a real object with least number of methods
  • Stubs return the pre-defined outputs no matter what the input is
  • These stubs are used to reduce complexities that occur while creating the real objects

--

--