4.10.2009

Unit Testing & Mocking with Easy Mock

If you are a fresh graduate worker for a corporate  company ,probably all dirty jobs that effect whole system are assigned to you. With an afraid of changing  the code, you read the code to find out where will be effected. Especially legacy systems can be so sophisticated that changing a piece of code, can make the whole system down in a second.

Testers, developers, project managers and customers do testing but the way of their tests are different. In this post i will not tell  how you can test whole system as a single entity, in direct contradiction I'm going to tell how you can test individual classes, methods by unit testing and dependent objects by mocking.

Eclipse provides  jUnit as a  default testing framework for  developers to write test codes in a little time by defining  just simple annotations. Fundamentally, JUnit provides three annotations; @setup which prepares pre testing conditions, @Test  which defines Service UnderTest, @After which prepares postConditions of the test.

To demostrate how it works, firstly i developed a single math class and a add method.

public class Math
{
public int add(int a, int b)
{
return a + b;
}
}



and wrote a three lines of code to test.




public class MathTest
{
private com.demo.dao.Math sut = null; //Service Under Test (SUT)

@Before
public void setup()
{
this.sut = new com.demo.dao.Math();
System.out.println("MathTest.setup()");
}


@Test
public void Add()
{
int a,b;
a = 10;
b = 30;

Assert.assertEquals(40, this.sut.add(a,b));
System.out.println("MathTest.Add()");


}

@After
public void tearDown()
{
this.sut = null;
System.out.println("MathTest.tearDown()");
}

}





So far so good. Think that days after days, you hire a new worker and assigned a issue to develop multiply function. Our worker will probably copy/paste the add code and will forget the change the + signal to *;  If our dummy worker can write test code for this math class he will see that multiply method does not work as he expected.




This is the easiest way of describing testing but in complex architectures it is not simple as i told. Because test code is optional (it is not what the customer wants), most of the developers will just develop automated tests for single, simple tests to satisfy project manager.





As you understand Unit testing helps developers the test the functionality of a function and class. Mocking means moving sophisticated dependencies from test code. Instead of initializing some objects and binding these objects to other objects  bore developers. By creating dummy mock objects, you make the class  tend to be the object that the SUT needs. But be careful mocking is not for testing functionality of a SUT, it is for testing the dependency of SUT.








package com.demo.dao;

public interface IUser
{
public void setLoginDao(IUserLoginDao dao);
public boolean login(String userName,String password);
}






package com.demo.dao;

public interface IUserLoginDao
{
public boolean login(String userName,String password);
}






package com.demo.dao;

public class UserService implements IUser
{


private IUserLoginDao userDao = null;

@Override
public boolean login(String userName, String password)
{
return this.userDao.login(userName, password);
}

@Override
public void setLoginDao(IUserLoginDao dao)
{
this.userDao = dao;

}
}

Easy Mock


1. It' is proxy based, so it is used to imitate the real object that SUT needs


2. Just writing 3 lines of code you initialize the proxy object.


image 
3. it is really easy.[http://www.jmock.org/easymock-comparison.html]


Hiç yorum yok: