分享
 
 
 

Junit Notes

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Author:Holyfair Chen

E-mail:plutocsj@hotmail.com

***************************

= 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;

4.RepeatTest;

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}"/>

<classpath>

<pathelement location="WEB-INF/classes/src"/>

<path refid="compile.classpath"/>

</classpath>

</junit>

</target>

<!-- ============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"/>

</fileset>

<report todir="${target.report.dir}/html"/>

</junitreport>

</target>

4.Also, you can run test in Eclipse with ant-build way.

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

==================================================================================

==================================================================================

**********************************

= Testing strategies =

**********************************

----------------------------------------------------------------------------------

【Stubbing】

definition:

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.

advantage:

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)

definition:

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

tests.

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

comments:

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.

<1>Mockrunner

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

conditions.

 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】

----------------------------------------------------------------------------------

【EJBS】

----------------------------------------------------------------------------------

【Struts】

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有