Skip to content

Introduction to JUnit – Part 2

July 23, 2013

In the previous part of Introduction to JUnit i explained how we can add JUnit to our java project and what are the primary annotations of JUnit.
In this part i explain how we can test our application with a simple example, if you remember i introduced an Object Factory. this Object Factory can build some objects and return them to clients.

Our Object Factory can be used in Connection Pools, Load Balancers and many other solutions.

Step 1:

We need to prepare out environment. In this tutorial i use maven. So create a new maven project. and add following dependency to your pom.xml


Then create a test class with name ObjectFactoryTest in your src/test/java directory of maven project.
Maybe it’s strange at first but in Test Driven Development at first we create test and then code just for passing our test. If you are not familiar with this kind of development read Test Before, Rest After!!

Step 2:

We need to write a test for our API and then write some code to pass it. So in this step we write our first test method.

private int NUM = 10;
 public void when_ObjectFactory_is_created_its_capacity_must_be_set(){
 final ObjectFactory objectFactory = new ObjectFactory(NUM);

Congratulation you have write your first unit test. There are some points in our test method.

first naming convention: there are two approach for naming test method, as you see in my test even i did not observe java method naming conventions. In this approach you completely explain what you are testing, so you have a good document for yourself and anyone who will read your codes.

The other approach that i don’t like it so 🙂 is the java classical convention, in this approach test method name must not be declarative and is just a name for your method, for our previous test method a good name in this model is : “testObjectFactoryCreation”.

It’s your choice that which one is better and you like to use, but at all i use first approach.

Second Assertion: I did not explain about test method assertion before, When you write a test it is obvious that you need to test! in JUnit the assertion will test your assumptions. so  Assert.assertEquals(NUM,objectFactory.getPoolSize()) means that i expect that return value of  getPoolSize must be equals to the NUM constant.

OK now that we write our first test we need to run it. in this level we have some compile errors, because we have not created ObjectFactory and its getPoolSize method yet. So try to resolve just compile errors and the execute: mvn clean test

The result is : Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

No you need to complete your ObjectFactory class to pass this test. Your ObjectFactory class will be something like this code:

public class ObjectFactory {
 private int poolSize;

public ObjectFactory(int poolSize) {
 this.poolSize = poolSize;

public int getPoolSize() {
 return poolSize;

You must continue with Test Code Refactor steps to complete your ObjectFactory code. ( in this post we don’t pay attention to Refactoing )

Clients need to get an object from our ObjectFactory, So we need to have a getObject method.

 public void getObject_should_not_return_null() {
 final ObjectFactory objectFactory = new ObjectFactory(NUM);
 final Object object = objectFactory.getObject();

For passing this test just you need to add follow method to your ObjectFactory class:

public Object getObject() {
 return new Object();

At the first glance you will understand that our code is not suitable for ObjectFactory but don’t forget you must write code just for passing your test. So the code is not OK because your tests are not enough.
When we call getObject more that poolSize we should get an existed Object not new one.

So we can write this test method:

 public void getObject_should_return_one_of_existing_objects() {
 final ObjectFactory objectFactory = new ObjectFactory(1);
 final Object firstObject = objectFactory.getObject();
 final Object secondObject = objectFactory.getObject();

Assert.assertSame("Two objects are not the same.",firstObject,secondObject);

For passing this unit test you need to change your getObject code,  Below code is one the solutions for passing test:

public Object getObject() {
 if (pool.size() < poolSize) {
 final Object object = new Object();
 return object;
 return pool.get(0);


pool field is a List reference to an ArrayList object ( You can see complete code in GitHub ).

But our  solution is not complete yet. When pool becomes full our code just return the first object!

So we can write this test method:

 public void getObject_should_return_different_objects_after_pool_becomes_full() {
 final ObjectFactory objectFactory = new ObjectFactory(2);
 final Object firstExistedObject = objectFactory.getObject();
 final Object secondExistedObject = objectFactory.getObject();

Assert.assertNotSame("Two objects are the same.",firstExistedObject,secondExistedObject);

Our test method emerges the absence in our code.Try to resolve this problem yourself 🙂 I will upload complete code in my github, but please try to complete codes yourself, it can be a good practice.

If you have any opinions or problems please give me feedback.


From → JAVA, JAVASE, Test

  1. hey there and thank you for your info – I have definitely
    picked up something new from right here. I did however
    expertise some technical issues using this website, as I experienced to reload the site many times previous to I could get it to load correctly.
    I had been wondering if your web hosting is OK?
    Not that I’m complaining, but slow loading instances times will very frequently affect your placement in google and can damage your high-quality score if ads and marketing with Adwords. Anyway I am adding this RSS to my e-mail and can look out for a lot more of your respective exciting content. Make sure you update this again very soon.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: