<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
><channel><title>Himmel &#187; unittest</title> <atom:link href="http://lazing.ave7.net/tag/unittest/feed/" rel="self" type="application/rss+xml" /><link>http://lazing.ave7.net</link> <description>时间很长；现在很短；距离很长；相遇很短</description> <lastBuildDate>Wed, 11 Jan 2012 18:03:14 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.2.1</generator> <creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license> <item><title>说一下EasyMock</title><link>http://lazing.ave7.net/2008/talk-about-easymock/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=talk-about-easymock</link> <comments>http://lazing.ave7.net/2008/talk-about-easymock/#comments</comments> <pubDate>Sat, 09 Feb 2008 17:16:56 +0000</pubDate> <dc:creator>某L</dc:creator> <category><![CDATA[Tech Forge]]></category> <category><![CDATA[easymock]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[unittest]]></category><guid isPermaLink="false">http://lazing.ave7.net/blog/?p=236</guid> <description><![CDATA[用了很长的时间，终于写出了合乎标准的单元测试。好吧，我就是做事情不够细致，所以才这么大费周折。。。 关于测试 做测试者，一定要弄清出不同种类测试的分别，弄清楚什么是Unit Test，什么是Intergration Test, Perfermance Test, Function Test. 不然，就相当于没有测试。 Unit Test是程序员完成，Intergration Test通常也是程序员。而Perfermance Test和Function Test则应当由专业的QA人员去做，当然如果有人多才多艺全部完成，那没有问题。 验收测试Validation Test 应当由用户按照SOW定义完成，当然，这也是理想的情况——我们有清晰的需求，严格控制的变更。应用XP的场合，这更多应当是庆功宴前的走形式。 为什么这么关心测试 测试是开发的标杆，里程碑，没有测试，就没有提交件。 测试是风险管理的最直接手段，良好的测试体系和执行力是软件项目中重要的技术性保障手段。 测试保证项目的完成，在项目发生变更时控制风险。 Martin Fowler写过一个故事 TestCancer ，描述了一个结构精巧，测试完美的应用如何生病而无可就要的。而我们一旦一时一刻放弃这些准则，就会陷入无以复加的尴尬境地，所以，在动手写代码前，特别是所谓的高级人员，先打好基本功，您的一个轻率决定，或者经验之谈，就会让整个团队陷入MASS. 关于EasyMock 使用了EasyMock感觉不错，就着代码说说使用中遇到的两个问题： 问题1 没有设置返回值 java.lang.IllegalStateException: missing behavior definition for the preceeding method call findByExample(&#60;any&#62;) 对于有返回值的方法，要设定预期返回值。 @Test public void testRegist() { User user = new User(); user.setUserId("testUser"); user.setPassword("password"); //下面这行代码去掉 .andReturn(null)就会报上面的错误。 <a href="http://lazing.ave7.net/2008/talk-about-easymock/"> read more <span class="meta-nav">&#187;</span></a><br /><div><img src="http://lazing.ave7.net/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br />]]></description> <content:encoded><![CDATA[<p>用了很长的时间，终于写出了合乎标准的单元测试。好吧，我就是做事情不够细致，所以才这么大费周折。。。</p><h4>关于测试</h4><ol><li>做测试者，一定要弄清出不同种类测试的分别，弄清楚什么是Unit Test，什么是Intergration Test, Perfermance Test, Function Test. 不然，就相当于没有测试。</li><li>Unit  Test是程序员完成，Intergration Test通常也是程序员。而Perfermance Test和Function Test则应当由专业的QA人员去做，当然如果有人多才多艺全部完成，那没有问题。</li><li>验收测试Validation Test 应当由用户按照SOW定义完成，当然，这也是理想的情况——我们有清晰的需求，严格控制的变更。应用XP的场合，这更多应当是庆功宴前的走形式。</li></ol><h4>为什么这么关心测试</h4><ol><li>测试是开发的标杆，里程碑，没有测试，就没有提交件。</li><li>测试是风险管理的最直接手段，良好的测试体系和执行力是软件项目中重要的技术性保障手段。</li><li>测试保证项目的完成，在项目发生变更时控制风险。</li></ol><p>Martin Fowler写过一个故事 <a href="http://martinfowler.com/bliki/TestCancer.html">TestCancer </a>，描述了一个结构精巧，测试完美的应用如何生病而无可就要的。而我们一旦一时一刻放弃这些准则，就会陷入无以复加的尴尬境地，所以，在动手写代码前，特别是所谓的高级人员，先打好基本功，您的一个轻率决定，或者经验之谈，就会让整个团队陷入MASS.</p><h4>关于EasyMock</h4><p>使用了EasyMock感觉不错，就着代码说说使用中遇到的两个问题：</p><p>问题1  没有设置返回值</p><blockquote><p>java.lang.IllegalStateException: missing behavior definition for the preceeding method call findByExample(&lt;any&gt;)</p></blockquote><p>对于有返回值的方法，要设定预期返回值。</p><pre name="code" class="java">
	@Test
	public void testRegist() {
		User user = new User();
		user.setUserId("testUser");
		user.setPassword("password");
		//下面这行代码去掉 .andReturn(null)就会报上面的错误。
		expect(mock.findByExample(anyObject())).andReturn(null);
		expect(pwe.encodePassword(eq(user.getPassword()), anyObject()))
				.andReturn("password");
		expect(mock.save(anyObject())).andReturn(1);

		replay(mock);
		replay(pwe);

		userService.regist(user);
	}</pre><p>问题2 参数匹配问题</p><blockquote><p>java.lang.IllegalStateException: 2 matchers expected, 1 recorded.</p></blockquote><p>对于EasyMock会对传入Mock对象方法中的参数进行比较，也就是调用equals或者==引用比较等等。如果不能正确设置比较的行为，就会造成参数不匹配。EasyMock提供了一些方法来定义如何匹配参数，请参考<br /> <a href="http://www.easymock.org/EasyMock2_3_Documentation.html" target="_blank"> EasyMock文档</a></p><pre name="code" class="java">
 @Test
 public void testJoinGame() {
 	Game expGame = new Game();
 	expGame.setId(1);
 	expGame.setCreateUser(new User());
 	expGame.setGameStatus(GameStatus.NOT_START);
 	expGame.setPlayer(new ArrayList());
 	//这行代码如果写成
 	// expect(publicDao.get(Game.class, anyLong())).andReturn(expGame);
 	//就会出现前文的错误
 	expect(publicDao.get(eq(Game.class), anyLong())).andReturn(expGame);
 	publicDao.update(expGame);
 	replay(publicDao);
 	gameService.joinGame(1, new User());
 	verify(publicDao);
 }</pre><ul><li><a href="http://lazing.ave7.net/2008/java-generating-word-documents-using-xml/" rel="bookmark" title="2008/03/01">Java：利用XML生成Word文档</a></li><li><a href="http://lazing.ave7.net/2008/java-scripting-in-jdk6/" rel="bookmark" title="2008/07/21">Java Scripting in JDK6</a></li><li><a href="http://lazing.ave7.net/2008/jruby-note-simple-is-beautiful/" rel="bookmark" title="2008/04/25">JRuby Note: 简单就是美</a></li><li><a href="http://lazing.ave7.net/2007/preamble-lazingu002639s-java-guide/" rel="bookmark" title="2007/06/23">序言 &#8211; Lazing&#8217;s Java Guide</a></li><li><a href="http://lazing.ave7.net/2007/swap-away/" rel="bookmark" title="2007/03/20">Swap Away</a></li></ul><p></p><p><a href="http://www.bshare.cn/share?url=http%3A%2F%2Flazing.ave7.net%2F2008%2Ftalk-about-easymock%2F&title=%E8%AF%B4%E4%B8%80%E4%B8%8BEasyMock" title="用bShare分享或收藏本文"><img src="http://static.bshare.cn/frame/images/button_custom1-zh.gif" alt="用bShare分享或收藏本文" /></a></p><br /><div><img src="http://lazing.ave7.net/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /> <img src="http://lazing.ave7.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=236" width="1" height="1" style="display: none;" />]]></content:encoded> <wfw:commentRss>http://lazing.ave7.net/2008/talk-about-easymock/feed/</wfw:commentRss> <slash:comments>2</slash:comments> <creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license> </item> <item><title>JUnit单元测试与集成测试指引</title><link>http://lazing.ave7.net/2007/junit-unit-testing-and-integration-testing-guidelines/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=junit-unit-testing-and-integration-testing-guidelines</link> <comments>http://lazing.ave7.net/2007/junit-unit-testing-and-integration-testing-guidelines/#comments</comments> <pubDate>Sat, 15 Dec 2007 09:40:07 +0000</pubDate> <dc:creator>某L</dc:creator> <category><![CDATA[Tech Forge]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Spring]]></category> <category><![CDATA[unittest]]></category><guid isPermaLink="false">http://lazing.ave7.net/blog/?p=209</guid> <description><![CDATA[基于JUnit 3.8 与Spring 1.2环境的测试方法 本文将介绍使用JUnit结合Spring进行单元测试和集成测试的一般方法，并探讨基本的测试最佳实践，希望能够协助大家提高代码质量。本文属于介绍性质，文后附有相关书目，可以系统的了解xUnit框架详细的知识。 文中源码供参考使用。 和本站所有文章一样，使用非商业性-署名-内容一致 有限保留版权，请参看创作共享协议3.0版本 文章和源代码下载：<br /><div><img src="http://lazing.ave7.net/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br />]]></description> <content:encoded><![CDATA[<p>基于JUnit 3.8 与Spring 1.2环境的测试方法</p><p>本文将介绍使用JUnit结合Spring进行单元测试和集成测试的一般方法，并探讨基本的测试最佳实践，希望能够协助大家提高代码质量。本文属于介绍性质，文后附有相关书目，可以系统的了解xUnit框架详细的知识。</p><p>文中源码供参考使用。</p><p>和本站所有文章一样，使用非商业性-署名-内容一致 有限保留版权，请参看创作共享协议3.0版本</p><p>文章和源代码下载：<a href="http://lazing.ave7.net/" title="JUnit&aring;<ul><li><a href="http://lazing.ave7.net/2008/java-generating-word-documents-using-xml/" rel="bookmark" title="2008/03/01">Java：利用XML生成Word文档</a></li><li><a href="http://lazing.ave7.net/2008/java-scripting-in-jdk6/" rel="bookmark" title="2008/07/21">Java Scripting in JDK6</a></li><li><a href="http://lazing.ave7.net/2007/java-module-and-maven/" rel="bookmark" title="2007/12/06">Java Module and Maven</a></li><li><a href="http://lazing.ave7.net/2009/teaching-paste-how-to-write-plug-ins-maven2/" rel="bookmark" title="2009/05/26">教学贴：如何编写Maven2插件</a></li><li><a href="http://lazing.ave7.net/2007/preamble-lazingu002639s-java-guide/" rel="bookmark" title="2007/06/23">序言 &#8211; Lazing&#8217;s Java Guide</a></li></ul><p></p><p><a href="http://www.bshare.cn/share?url=http%3A%2F%2Flazing.ave7.net%2F2007%2Fjunit-unit-testing-and-integration-testing-guidelines%2F&title=JUnit%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E4%B8%8E%E9%9B%86%E6%88%90%E6%B5%8B%E8%AF%95%E6%8C%87%E5%BC%95" title="用bShare分享或收藏本文"><img src="http://static.bshare.cn/frame/images/button_custom1-zh.gif" alt="用bShare分享或收藏本文" /></a></p><br /><div><img src="http://lazing.ave7.net/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /> <img src="http://lazing.ave7.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=209" width="1" height="1" style="display: none;" />]]></content:encoded> <wfw:commentRss>http://lazing.ave7.net/2007/junit-unit-testing-and-integration-testing-guidelines/feed/</wfw:commentRss> <slash:comments>2</slash:comments> <creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license> </item> </channel> </rss>
