Skip to content Skip to sidebar Skip to footer

Simple Junit Test Case Example in Java

java-junit-example

What is JUnit?

JUnit is a simple, powerful, open source framework to write and run repeatable tests. I love JUnit testcases. During my Java Project development, I extensively use JUnit for code coverage. It is an instance of the xUnit architecture for unit testing frameworks.

JUnit features include:

  1. Assertions for testing expected results
  2. Test fixtures for sharing common test data
  3. Test runners for running tests

In this tutorial I'll cover point 1 and 3. You need below maven dependancies in your project.

Let's begin writing code.

Step-1

Create projectCrunchifyJunitTest and specify package com.crunchify.junit. Here is a Package structure for quick reference.

Crunchify JUnit Project Eclipse Structure

Step-2

Right click on CrunchifyJunitTest => New Class =>CrunchifyJunitTest.java

Step-3

Eclipse will suggest and automatically addorg.junit.Test dependency once you type @Test.

Eclipse Test Maven Dependency

Step-4

To test your JUnit test

  • Right click on class
  • Click on Run As
  • Click on JUnit Test

You should see something like this.

JUnit Test successful

Step-5

Try to change method expected value param from 30 => 300 and from Hello World => Hello -- World and test again.

JUnit Test failure - Crunchify Tips

Step-6

Now lets run the same test case via other java program. CreateCrunchifyRunnerJUnit.java

Step-7

Run CrunchifyRunnerJUnit.java as a Java Application.

Step-8

In case of 1st code (correct expected param), you should see output like this:

Step-9

In case of 2nd code (incorrect expected param), you should see something like this:

And you are all set. Do let me know if you see any trouble running this code.

JUnit Tutorial by Crunchify

Available Annotation in JUnit testcase:

Annotation

Description

@Test public void method() The annotation @Test identifies that a method is a test method.
@Before public void method() Will execute the method before each test. This method can prepare the test environment (e.g. read input data, initialize the class).
@After public void method() Will execute the method after each test. This method can cleanup the test environment (e.g. delete temporary data, restore defaults).
@BeforeClass public void method() Will execute the method once, before the start of all tests. This can be used to perform time intensive activities, for example to connect to a database.
@AfterClass public void method() Will execute the method once, after all tests have finished. This can be used to perform clean-up activities, for example to disconnect from a database.
@Ignore Will ignore the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included.
@Test (expected = Exception.class) Fails, if the method does not throw the named exception.
@Test(timeout=100) Fails, if the method takes longer than 100 milliseconds.

Available Assert Test Methods:

Statement

Description

fail(String) Let the method fail. Might be used to check that a certain part of the code is not reached. Or to have failing test before the test code is implemented.
assertTrue(true) / assertTrue(false) Will always be true / false. Can be used to predefine a test result, if the test is not yet implemented.
assertTrue([message], boolean condition) Checks that the boolean condition is true.
assertsEquals([String message], expected, actual) Tests that two values are the same. Note: for arrays the reference is checked not the content of the arrays.
assertsEquals([String message], expected, actual, tolerance) Test that float or double values match. The tolerance is the number of decimals which must be the same.
assertNull([message], object) Checks that the object is null.
assertNotNull([message], object) Checks that the object is not null.
assertSame([String], expected, actual) Checks that both variables refer to the same object.
assertNotSame([String], expected, actual) Checks that both variables refer to different objects.

Share:

I'm an Engineer by profession, Blogger by passion & Founder of Crunchify, LLC, the largest free blogging & technical resource site for beginners. Love SEO, SaaS, #webperf, WordPress, Java. With over 16 millions+ pageviews/month, Crunchify has changed the life of over thousands of individual around the globe teaching Java & Web Tech for FREE.

keenerfachaps.blogspot.com

Source: https://crunchify.com/simple-junit-4-tutorial-hello-world-example/

Enviar um comentário for "Simple Junit Test Case Example in Java"