Junit Notes

原创 2004年10月15日 10:16:00

Author:Holyfair Chen

=  Something about JUnit  =
1.JUnit (junit.org) is open source software, released under IBM’s Common Public
  License Version 1.0
2.Three kinds of Runners to choose: junit.textui.TestRunner ;
                                    junit.awtui.TestRunner ;
                                    junit.swingui.TestRunner ;
3.TestCase: Setup() ---> testXXX()------>tearDown()

4.You can add some TestCases to one TestSuite and run them together.The TestRunner
  starts the tests by calling the TestSuite’s run(TestResult)method.
5.A TestResult advertises:  A test has been started (startTest; see E).
     A test has failed (addFailure; see figure 2.10).
     A test has thrown an unexpected exception
        (addError; see figure 2.10).
     A test has ended (endTest; see figure 2.10).
6.The TestCases in a TestSuite can be run orderly.Also you can write your own TestSuites
  to run those TestCases randomly.
7.Junit can also test private class methods.(using the PrivilegeAccessor.java)

8.junit.extension package: 1.test result report;
      2.use ActiveTestSuite to deal with multiThread Test;
      3.ExceptionTestCase:if no Exception then report Error;
      5.TestSetup: the public enviroment initialize;


= Automating JUnit =

1.You can run Junit from Ant, Maven,and Eclipse;

2.Eclipse: download Eclipse-Junit-plugin,put it in the folder "{Eclipse-HOME}/plugin/";

3.ant: pre:cp the junit.jar to "{ANT_HOME}/lib"
 a example:
  <!-- =============Test Targets=========== -->
  <target name="test" depends="compile">
   <mkdir dir="${target.report.dir}"/>
   <junit printsummary="yes" haltonerror="no" haltonfailure="no" fork="yes">
    <formatter type="plain" usefile="false"/>
    <formatter type="xml"/>
    <test name="test.Mock.TestMainSuite" todir="${target.report.dir}"/>
      <pathelement location="WEB-INF/classes/src"/>
      <path refid="compile.classpath"/>

  <!-- ============Report Targets========== -->
  <target name="report" depends="test">
   <mkdir dir="${target.report.dir}/html"/>
   <junitreport todir="${target.report.dir}">
    <fileset dir="${target.report.dir}">
     <include name="TEST-*.xml"/>
    <report todir="${target.report.dir}/html"/>
4.Also, you can run test in Eclipse with ant-build way.

5.JUnit best practices: same package, separate directories;


=  Testing strategies  =
     A stub is a portion of code that is inserted at runtime in place of
 the real code, in order to isolate calling code from the real implementation.
 The intent is to replace a complex behavior with a simpler one
 that allows independent testing of some portion of the real code.

 1.Stubs are a mechanism for faking the behavior of real code that may exist
   or that may not have been written yet.Stubs allow you to test a portion
   of a system without the other part being available.
 2.They usually do not change the code you’re testing but instead adapt to
   provide seamless integration.

Tools mentioned:
 1. Using "Jetty" as an embedded server;

comments:stubbing a web server, the filesystem, a database, and so on

【Mock Objects】  (isolate a method call to another class,used as Trojan horses)
    (outside-the-container testing)
       A mock object (or mock for short) is an object created to stand in for
  an object that your code will be collaborating with. Your code can call
  methods on the mock object, which will deliver results as set up by your
Technology mentioned:
 1. Refactoring: <1>easy method refactoring technique
   <2>refactoring by using a class factory

When to use:
 * Real object has non-deterministic behavior;
 * Real object is difficult to set up;
 * Real object has behavior that is hard to cause;
 * Real object is slow;
 * Real object has a UI;
 * Test needs to query the object, but the queries are not available in the real
   object (for example, “was this callback called?”)
 * Real object does not yet exist

 1.The MockObjects project(
http://www.mockobjects.com)provides some ready-made
 mock objects for stantard JDK APIs. The mocks usually have expectations built
 in.In addtion, the MockObjects project contains some reusable expectation
 classes that you can use in your own mocks.
 2.present several open source frameworks that automatically generate ready-to-use
 mocks for your classes  make it a pleasure to use the mock-objects strategy.
   Mockrunner is a lightweight framework for unit testing applications
          in the J2EE environment. It supports Struts actions and forms,
          servlets, filters and tag classes. Furthermore it includes a JDBC
           test framework. The JDBC test framework can be used standalone
            or in conjunction with MockEJB to test EJB based applications.
  <2>MockEJB ,EasyMock ,Mock Objects MockMacker,DynaMock,etc;

 3.mock-object generation tool:
  MockMacker can be the Eclipse Plugin!!!

【Cactus】  (In-Container Testing)
  URL concerned:http://jakarta.apache.org/cactus/
  **Mock objects and Cactus are both valid approaches.**

 Executing the tests using 【Cactus/Jetty integration】
 Code example:
 public static Test suite()
  TestSuite suite = new TestSuite("All tests with Jetty");
  suite.addTestSuite(TestCaseExendXXXTestCase.class); //( xxx can be Servlet,
  //Jsp, or Filter
  return new JettyTestSetup(suite);

Life cycle of a Cactus test:
 execute begingXXX()---it. The beginXXX method lets you pass information to the redirector
 open the proxy-redirector connection
 create the server-side TestCase instance
 call setUp, testXXX, and tearDown on the server side
 execute endXXX ---This method is used so that your tests can assert additional results from the
 code under test.
 Gathering the test result

Drawbacks of in-container testing:
 1. Specific tools required;
 2. Longer execution time;
 3. Complex configuration.



= Testing components =   (J2EE components)

【【When to use Cactus, and when to use mock objects】】 
 At this point, you must be wondering whether to use Cactus or mock objects to
 test your servlets and filter. Both approaches have advantages and disadvantages:
   The main difference is that Cactus performs not only unit tests but also
  integration tests and, to some extent, functional tests. The added benefits
  come at the cost of added complexity.
   Mock-object tests are usually harder to write, because you need to define
  the behavior of all calls made to the mocks. For example, if your method
  under test makes 10 calls to mocks, then you need to define the behavior
  for these 10 calls as part of the test setup.
   Cactus provides real objects for which you only need to set some initial
   If the application to unit-test is already written, it usually has to be refactored
  to support mock-object testing. Extra refactoring is generally not
  needed with Cactus.
 A good strategy is to separate the business-logic code from the integration code
 (code that interacts with the container), and then:
   Use mock objects to test the business logic.
   Use Cactus to test the integration code.

Other components testing can be know from the book: <Junit in Action>
【servlets & filters】

【JSPs & taglibs】

【database application】




jUnit Notes

1. java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass Solution:  htt...
  • Derek_Zhang_
  • Derek_Zhang_
  • 2012年12月05日 16:46
  • 448


C:/Notes/Notes.exe =C:/Notes/MyNotes.ini根据mynotes.ini启动clientC:/Notes/Notes.exe dbName.nsf当notes cli...
  • snowdot23
  • snowdot23
  • 2004年08月20日 14:30
  • 1160

47. Lotus Notes中编程发送邮件(二)

  • starrow
  • starrow
  • 2013年08月17日 17:00
  • 6939

java调用notes API 发送邮件

最近需要调用Notes java API来发送邮件.主要参考了以下两个博客: 1   http://www.blogjava.net/jiangmin/articles/76137.html这个博客完...
  • lihe2008125
  • lihe2008125
  • 2010年02月22日 17:37
  • 2319

重新配置lotus Notes 9.0客户端

路径:C:\Users\用户名\AppData\Local\Lotus\Notes\Data 1.删除 Names.nsf /  Desktop8.ndk (or Desktop6.ndk) /  ...
  • dongshibo12
  • dongshibo12
  • 2017年09月07日 16:14
  • 311

[转贴] 通过命令行启动Notes客户端

环境:(产品,平台,机型,软件版本,等)产品:Lotus Notes平台:Windows版本:6.x,5.x,4.x问题描述:启动Notes客户端的时候,什么样的命令行参数可以传到Notes.exe程...
  • tttk
  • tttk
  • 2007年08月03日 08:34
  • 1227

在 Lotus Notes 中设置邮件定时发送的方法及代理功能介绍

孙 茂伟, 测试工程师, IBM 孙茂伟在北京科技大学获得工学学士、工学硕士学位,现于 IBM CDL ECM FileNet EForms TEAM 从事自动化测试相关工作。   简介: 本文...
  • hanle18
  • hanle18
  • 2011年10月19日 14:08
  • 1861

C#简单操作Lotus Notes邮件

前段时间简单的研究了一下.NET操作Lotus Notes邮件的实现,具体的操作包括邮件的读取和发送,而且都要包含附件,其间参考了《在 Microsoft .NET 应用程序中使用 IBM Lotus...
  • sinat_27305053
  • sinat_27305053
  • 2017年02月25日 17:17
  • 1063


Lotus Domino/Notes是美国莲花公司出的企业通讯和群件服务平台。但由于其本身提供的开发工具的限制,在Notes中实现系统级功能十分困难。比如在Notes中,不能通过公式或脚本来得到一个数...
  • gzxiaohao
  • gzxiaohao
  • 2004年11月24日 10:03
  • 2105

基础知识Notes: 字符串编码(ASCII, Unicode and UTF8)

总结陈述:ASCII码是用来表示英语字符的编码。只用1个字节(8位)。所以总共有256种可能。虽然实际而言只规定了128种,但是其每个字符的表示依然需要1个字节。而且,最高位永远为0. UT...
  • Firehotest
  • Firehotest
  • 2017年02月20日 07:50
  • 392
您举报文章:Junit Notes