Junit Notes

xiaoxiao2021-03-06  92

Author: Holyfair CHENE-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.teestrunner; junit.awtui.teestrunner 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 Privileaccessor.java)

package 8.junit.extension: 1.test report result; ActiveTestSuite to deal with multiThread Test 2.use; 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: 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 this is inserted at runtime in place of the real code, in order to isolate call code from the ................... ..

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 Seamness 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 OBAED (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] 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 resultDrawbacks 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 t he 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:. --- -------------------------------------------------- ----------------------------- [Servlets & Filters]

-------------------------------------------------- -------------------------------- [JSPS & TAGLIBS]

-------------------------------------------------- -------------------------------- [Database Application]

-------------------------------------------------- -------------------------------- [EJBS]

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

[Struts]

转载请注明原文地址:https://www.9cbs.com/read-106082.html

New Post(0)