Skip to content

Introduction to JUnit

July 19, 2013

If you have never write test, it must be a concern for you ” When can i be sure that my code works “Most of the times you have to realize many parts of your software until you could validate your code! But there is another way to validate the accuracy of you code. You can write a simple client for your code and validate the behavior of it.
For example you want to create a special Object Factory. this Object Factory must create a specified numbers of objects and in the next requests to get an instance it must get objects with a round robin algorithm,

As you see this Object Factory is not very hard to write, and you will its testing is not hard too. But if you have a web project and you don’t want to have automated tests maybe you need these augmented parts of software for testing:

  • Your API must be complete without client code ( You need to think as client when you are writing your API )
  • All the services you need to use your Object Factory
  • Web UI as a client of your code
  • Some Not required logging for watching the functionality
  • Debug your code ineffective for finding the place of bugs

If you are agree with me that these are problem continue to reading this post πŸ˜‰ in the other way continue too maybe you find out a more simple solution for your accuracy.

JUnit is the most popular testing framework in the java world. The last major version of this framework is 4.x. One of the biggest changes in this version is using Annotation instead of conventions. When you want to test a code you write a class ( Test Class ) and write your tests ( Test Methods ) for the assertion of your code. You can see a simple Test Template in the following code:

public class ObjectFactoryTest {

@Test

public void ObjectFactory_should_create_with_specified_numbers_of_pool() {

//Arrange code

//Act Code

// Assert Code

}

}

So a Test Class is just a simple class with special annotations the JUnit Framework can understand them and run your tests. Some basic annotations that is desirable to know now for you are :

  • BeforeClass: A Static method that will run at the first of starting the tests of your test class, So this static method just run one time for your test class
  • AfterClass: A Static method that will run after running all test methods of your test class, So this static method just run one time for your test class too
  • Before: A method that will run before every test method of your test class.
  • After: A method that will run after every test method of your test class.
  • Test: This annotation specify your test methods

If you like to know how they run you can run following code ( Source code is from: JUnit 4 Tutorial 1 – Basic usage )

import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;

/**
 * @author mkyong
 *
 */
public class JunitTest1 {

    private Collection collection;

    @BeforeClass
    public static void oneTimeSetUp() {
        // one-time initialization code
    	System.out.println("@BeforeClass - oneTimeSetUp");
    }

    @AfterClass
    public static void oneTimeTearDown() {
        // one-time cleanup code
    	System.out.println("@AfterClass - oneTimeTearDown");
    }

    @Before
    public void setUp() {
        collection = new ArrayList();
        System.out.println("@Before - setUp");
    }

    @After
    public void tearDown() {
        collection.clear();
        System.out.println("@After - tearDown");
    }

    @Test
    public void testEmptyCollection() {
        assertTrue(collection.isEmpty());
        System.out.println("@Test - testEmptyCollection");
    }

    @Test
    public void testOneItemCollection() {
        collection.add("itemA");
        assertEquals(1, collection.size());
        System.out.println("@Test - testOneItemCollection");
    }
}

If you want to use JUnit with maven you need to add following dependency to your pom.xml

<dependency>

<groupid>junit</groupid>

<artifactid>junit</artifactid>

<version>4.8.2</version>

</dependency>

Don’t forget as a convention maven runs test classes that have Test Suffix. So your Test Class Names must be *Class.java format.

Now it’s your time to use JUnit and executed some simple tests. You can write Object Factory code with Test First Codes. In the next tutorial i will explain one of the possible implementations of Object Factory and its tests.
Please give me feedback if somewhere exists ambiguity or you need any prerequisites for these tutorials.

Advertisements

From → JAVA, JAVASE, Test

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: