Monday May 01, 2006 First Experience with EasyMock
I like test driven development. But I don't write tests for all features of my code because it is difficult to setup testing environment. For example it is difficult to set up unit test environment for NetBeans Java Model (API). There are too many dependencies. It is possible to run tests in running NetBeans (IDE mode). But I don't prefer this variant because it takes a lot of execution time. Therefore I didn't write tests for pieces code which uses JMI API.
I found regression in my code which work quite long time. So I designed to create Mock objects for few JMI interfaces. Mock objects is implementation of classes for testing purpose. Simply you write tests against API without its implementation, It's mean that you define a scenario of use case of API's pieces in your test. For example you want to to develop tests for application which uses jdbc connection to access data. To setup database is quite difficult. So you write tests without database. You need to simulate jdbc. In this case you can use already implemented mock objects for jdbc api. Developing JDBC applications test-first is nice article about mocking jdbc. Writing of test is simple when jmock library already exists for the API. But in many cases there is no jmock implementation for API which you are using and the api is not well designed for testing. But you want to write tests at least for the critical code. You will have to write jmock objects yourself. Fortunately you can use library for writing jmock object. I know two libraries for Java languages for this purpose:
I tried to use JMock library in the past. You can mock interface and classes. I don't like two things on JMock. JMock uses strings for mocked method names. I have bad memory and I am alredy got a customed to use code completion. The jmock was dessigned for junit 3.8. The recomended way how to use jmock is to use jmock's class like supperclass for yor test case. You don't need to extends your testcase but the code is not then so nice. Today I used Easy Mock it the first time. It is dessigned for TestNG and JUnit 4. It supports jdk 1.5. You can use static import if you don't want to write the classname in everytime. You can use code completion when you define the behavior of mocked code.
I describe my first unittest for code with JMI usage. Look at the code. The comments is bellow.
import java.util.*;
import junit.framework.*;
import org.netbeans.jmi.javamodel.Method;
import org.netbeans.junit.*;
import static org.easymock.EasyMock.*;
public class ElementTest extends NbTestCase {
....
public void testParseUnknownTag() {
Method m = createMock(Method.class);
expect(m.getJavadocText()).andReturn("@myTag");
expect(m.getAnnotations()).andReturn(Collections.EMPTY_LIST);
replay(m);
Element el = Element.findElement(m);
assertNotNull(el);
Tag tag = el.getTag("myTag");
assertNotNull(tag);
assertTrue(tag.getTagTemplate().isUnknown());
assertTrue(m instanceof Method);
}
...
}
In the code is tested parsing javadoc tags from Method element. The test is separed into three parts. In the first part is created jmock object for method element. The method contains javadoc with value "/**@myTag*/" and no annotations. No more methods is used in tested code so it is not necessary to have more mocking methods. The second part activates the mock object by invoking replay(m) method. The code is tested in the last part.
By EasyMock you can also check count of calls. Let's show example. A method's call is expensive. To be sure that the method is called only one time you can write mock object for the class of method. In the test will be tested if the method is called only one time.
Posted by xzajo ( May 01 2006, 05:38:51 PM CEST ) Permalink Comments [4]
Posted by 水无痕 on July 21, 2007 at 07:01 AM CEST #
The best of luck! Best wishes! Best regards!
http://www.bjseek.com.cn
http://www.bjxlzx.cn
http://www.sense.com.cn
http://jglsx.blog.sohu.com/
http://blog.china.alibaba.com/blog/jglsx.html
http://jglsx.blog.hexun.com/
http://tw.netsh.com/eden/blog/ctl_eden_blog.php?iBlogID=2610970
http://tw.netsh.com/eden/blog/ctl_eden_blog.php?iBlogID=2632783
http://blog.soufun.com/blog_12178456.html
http://blog.china-pub.com/blog.asp?name=jlb148140960
http://www.phpchina.com/38743
http://hexun.com/jglsx/default.html
http://www.xiongcaocao.com/?sid=116
http://jlb999.blog.163.com/
http://jjllbb.eomoo.com/user18/jlb148140960/index.shtml
http://hexun.com/xicao1/default.html
http://hexun.com/xicao3/default.html
http://hexun.com/xicao6/default.html
http://hexun.com/xicao9/default.html
http://hexun.com/xicao2/default.html
http://hexun.com/xicao7/default.html
http://hexun.com/xicao4/default.html
http://hexun.com/xicao5/default.html
http://hexun.com/xicao8/default.html
http://hexun.com/zwwsl/default.html
http://xicao6.blog.hexun.com/
http://xicao7.blog.hexun.com/
http://xicao8.blog.hexun.com/
http://xicao9.blog.hexun.com/
http://zwwsl.blog.hexun.com/
http://xicao1.blog.hexun.com/
http://xicao2.blog.hexun.com/
http://xicao3.blog.hexun.com/
http://xicao4.blog.hexun.com
http://xicao5.blog.hexun.com
http://tzgoogle.blog.hexun.com
http://hexun.com/tzgoogle/default.html
http://blog.ccidnet.com/blog-htm-uid-71265.html
http://hexun.com/baom/default.html
http://baom.blog.hexun.com
http://www.0576w.cn/
http://www.0576w.cn/catalog.asp?cate=1
http://www.0576w.cn/catalog.asp?cate=2
http://www.0576w.cn/catalog.asp?cate=3
http://www.0576w.cn/catalog.asp?cate=5
http://www.0576w.cn/catalog.asp?cate=6
http://www.0576w.cn/post/google.html
Posted by 数据恢复 on February 14, 2008 at 01:45 AM CET #
http://www.wowgolds.co.uk wow gold http://www.wowgolds.co.uk cheap wow gold http://www.wowgolds.co.uk buy wow gold http://www.wowgolds.co.uk world of warcraft gold http://www.wowgolds.co.uk wow http://www.wowgolds.co.uk world of warcraft http://www.wowgolds.co.uk/wow-gold.html wow gold http://www.wowgolds.co.uk/warrior.html WoW Warrior http://www.wowgolds.co.uk/hunter.html WoW Hunter http://www.wowgolds.co.uk/rogue.html WoW Rogue http://www.wowgolds.co.uk/paladin.html WoW Paladin http://www.wowgolds.co.uk/shaman.html WoW Shaman http://www.wowgolds.co.uk/priest.html WoW Priest http://www.wowgolds.co.uk/mage.html WoW Mage http://www.wowgolds.co.uk/druid.html WoW Druid http://www.wowgolds.co.uk/warlock.html WoW Warlock http://www.powerleveling2.cn power leveling http://www.powerleveling2.cn powerleveling http://www.wowpowerleveling2.cn wow power leveling http://www.wowpowerleveling2.cn wow powerleveling http://www.wowpro.cn wow guides http://www.wowmmo.cn wow tips http://ggtop.nbatop.com google排名 http://ggtop.nbatop.com google左侧排名 http://ggtop.nbatop.com google排名服务 http://www.baidupaiming.net 百度推广 http://www.baidupaiming.net 百度排名 http://www.fzqsnet.cn 网站推广 http://www.thebiz8.com 商业吧 http://www.thebiz8.com/jichuang.html 机床 http://www.thebiz8.com/leddeng.html LED灯 http://www.thebiz8.com/dianchi.html 电池 http://www.thebiz8.com/suliao.html 塑料 http://www.thebiz8.com/shexiangji.html 摄像机 http://www.thebiz8.com/yimin.html 移民 http://www.thebiz8.com/jiachun.html 甲醇 http://www.thebiz8.com/ranliao.html 染料 http://www.0591qs.cn 福州热线 http://www.sportblog.org.cn 体育博客 http://www.stockblog.org.cn 股票博客 http://www.gameblog.org.cn 游戏博客 http://www.cheap-wow-gold.org.cn 魔兽博客 http://www.excamtest.cn 考试博客 http://www.carblog.org.cn 汽车博客 http://www.houseblog.org.cn 房产博客 http://www.pcblog.org.cn 电脑博客 http://powerlin518.iblog.com powerlin518 http://www.xcelwebdesign.com logo design http://www.xcelwebdesign.com website design http://www.xcelwebdesign.com web design http://www.shangbiaodesign.cn 商标设计
Posted by power leveling on March 20, 2008 at 05:04 PM CET #
http://sunnyren.weblog.com/
this blog introduces jewelry products and fashion jewelry
Posted by fashion jewelry on October 31, 2008 at 06:58 AM CET #