Title: JUnit Framework ? Cactus
1JUnit Framework ? Cactus
?????????? ????????
2??????????
- ????????? ? JUnit
- JUnit ? Ant
- ????????? ? Cactus
- ?????????? ?? JUnit
- Q A
3????? ? JUnit
- ???????? (???????) ? Java Unit Testing
- ???????? ???? 1997 ?? Erich Gamma Kent Beck ??
?????? ?? Smalltalk (SUnit) - ?????? ?????? 3.8.1
- ??????? ??? (CPL v1.0)
- http//www.junit.org
4??????????? ?? JUnit
- http//prdownloads.sourceforge.net/junit/junit3.8.
1.zip?download - ?????????? ?? junit3.8.1.zip
- junit.jar
- src.jar
- junit\
- javadoc\
- doc\
- .
- ????? ?????????? ?? junit
- java -classpath junit.jar. junit.textui.TestRunne
r junit.tests.AllTests
5???????? ?? JUnit
????? ???????/ ?????????? TestResult TestListene
r BaseTestRunner Text Swing AWT .....
6??????? ???????
- Test Fixture
- ??????? ? ????? ?? ?????????? junit ???????
- Test case
- ???? TestCase
- Test suite
- ???? TestSuite
- Assertions
- ???? Assert
- Test Runner
- junit.textuiawtuiswingui.TestRunner
- ?????????? BaseTestRunner
7??????? ?????? ?? TestCase
- ????????????
- public TestCase()
- public TestCase(String name)
- ??????
- protected void setUp()
- protected void tearDown()
8??????? ?????? ?? TestSuite
- ????????????
- public TestSuite()
- public TestSuite(Class theClass)
- public TestSuite(Class theClass, String name)
- public TestSuite(String name)
- ??????
- public void addTest(Test test)
- public void addTestSuite(Class testClass)
9??????? ?????? ?? Assert lt1gt
- ?????? assertXXX()
- public static void assertEquals(String message,
Type1 expected, Type1 actual ,Type2 delta) - Type1 byte, char, double, float, int, long,
short, Object, String - public static void assertEquals(String message,
int expected, int actual) - public static void assertEquals(String expected,
String actual) - Type2 double, float, Type 2 Type1.
???????????? ??? Type1 double ??? float - assertFalse, assertTrue, assertNotNull,
assertNull, assertNotSame, assertSame
10??????? ?????? ?? Assert lt2gt
- ?????? fail(.)
- public static void fail()
- public static void fail(String message)
- ???????? assertXXX(.)
- ????????? ?????? AssertionFailedError ??????
????????? ?? ? ?????????. - ???????? fail()
- ????????? ?????? AssertionFailedError ??????.
11JUnit ? ???????? lt1gt
- ????? junit ??st Case ?????? ?? ?????????
junit.framework.TestCase - ????? ???? ?? ???????? ???? public void ?????.
- ????? ?? ?????? ?????? ?? ??????? ? test
- ??? ?????????? ?? ???? ??????? ??? ????? ?????
???? ?? ?????? ???????????.
12?????? ????? ?? Test Case
- ?? ????? public void testXXX() ????? ?? ???? Test
Case ?? ?????????
testXXX()
tearDown()
setUp()
- ??? setUp(), testXXX() ? tearDown() ?? ?????????
??????(Throwable), ????? ? ???????. ? ????????
?????? ????? ? ?????????. - ??????? ?/? Failure ? Error
- AssertionFailedError - ????????? ?? ???? Failure
- ????? ?????????? (????????? ?? ???? Error).
13JUnit ? ???????? lt2gt
- package org.devbg
- import junit.framework.TestCase
- public class TestCalculator extends TestCase
- Calculator calc
- protected void setUp() throws Exception
- System.out.println("setUp() method")
- calc new Calculator()
-
-
14JUnit ? ???????? lt3gt
-
- protected void tearDown() throws Exception
- System.out.println("tearDown() method")
-
- public void testAdd()
- System.out.println("testAdd() method")
- assertEquals(4, calc.add(2,2))
-
- public void testSub()
- System.out.println("testSub() method")
- assertEquals(0, calc.sub(2,2))
-
-
15?????????? ?? ??????? (Test Runners)
- Junit ???????? ??? ?????? ?? ?????????? ??
??????? - junit.textui.TestRunner
- junit.awtui.TestRunner
- junit.swingui.TestRunner
- ?????????? junit.runner.BaseTestRunner
16??? ?????? ???? Test Runner
- public static void main(String args)
- ?????? ? args ??? ?? ????, ?????
- ??? ????? public static Test suite()
- ????? Test Suite ?? ??????????.
- ????????? ?est Case
- ?????? ??????????? ?? ??????? Test Suite, ?????
??????? ?????? public void test.() ???? ???????. - ???????? Reflection ?? ?? ?????? public void
testXXX() ????????. - ????? ???? ?? ????????? ?????????? ?? ???????.
- ???? ?? ?? ?????? ?????????
- junit.textui.TestRunner().run(Test test)
- junit.textui.TestRunner().run(Class class)
17Demo Test Case, Suite, Runner
DEMO
18Keep the bar green
19????? ???????? ??????? lt1gt
- ??? ?? ??????? protected ??????
- ????????? ?????? ?? ?? ? ?????? ?????? ????
????????? ????? ?? ??????? - ??? ?? ??????????? ?????????
- src\com\xx\yy\zz - ???
- test\com\xx\yy\zz - ???????
- ??? ?? ??????? private ??????
- ?? ? ????? ???????? ?? ?? ??????? private ??????.
- ?? ??? ??? ??? ?? ?????? ?????????? ?? private
?????? - http//www.artima.com/suiterunner/private
.html
20????? ??????? ??????? lt2gt
- ??? ?? ????????? ?????? Test Cases ? ??????
- ????????? Test Suite ????? ??????? ?????? test
cases. 2 ?????? - ??????????
- ? Reflection
- ??????????? Ant.
21??????????
- ????????? ? JUnit
- JUnit ? Ant
- ????????? ? Cactus
- ?????????? ?? JUnit
- Q A
22Ant
- ????? ???????? ?? ?????????????? ?? ?????????
?????? ???? ???????????, ????????? ?? Java doc,
??????????, ??????????. - ???????????????? ?? ???????? ??????? ???? xml
??????????????? ????. - ???????? (???????) ? Java
- ?????? ?????? 1.6.2
- http//ant.apache.org
23JUnit ? Ant
- ?? ?? ?????? Ant ? junit ?????? junit.jar ?? ? ?
???????????? libs ?? ?nt. - ltjunitgt task
- ltjunitreportgt task
24ltjunitgt task
- ltjunit haltonfailureno haltonerrorno"gt
- ltclasspathgt
- ltpathelement location"dist"/gt
- lt/classpathgt
- ltformatter type"xml"/gt
- .
- lt/junitgt
Classpath ?? ???????????? ?? ?????????
25ltformattergt
- ????? ?? ? ??????? ?? ????????? ? ???? ?? ??
??????? ??? ????. - ltformatter typexmlplainbriefgt
- ???????
- ltformatter type"xml"/gt
- ltformatter type"brief"/gt
26lttestgt ? ltbatchtestgt
- ltjunitgt
-
- lttest fork"yes" name"TestAll"
todir"junit.report"/gt - ltbatchtest fork"yes" todir"junit.report"
gt - ltfileset dir"test"gt
- ltinclude name"/Test.java"/gt
- ltexclude name"/TestAll.java"/gt
- lt/filesetgt
- lt/batchtestgt
- .
- lt/junitgt
yes ???????? ???? ????????? ?? JVM ?? ?????
???? ????? no ????????? ????????? ? ????????
JVM
???? ?? ????? ?????????
27ltjunitreportgt task
- ltjunitreport todir"junit.report"gt
- ltfileset dir"junit.report"gt
- ltinclude name"TEST-.xml"/gt
- lt/filesetgt
- ltreport format"frames todir"junit.report
/html"/gt - lt/junitreportgt
- ltreportgt ???????? ????????????? XSL
?????????????. ??????? styledir. - junit-frames.xsl.
- junit-noframes.xsl
- ?????? ltreport formatframes
styledirc/xslt/gt
28JUnit Ant
DEMO
29?????????? ?? Ant ? JUnit
- ????? ??????????
- ????? ?????????? ?? ?????? ??????? ? ???????
- ?????????????? ????????
- ????????? ???????? ? ????
- Refactoring
- Regression Testing
30??????????
- ????????? ? JUnit
- JUnit ? Ant
- ????????? ? Cactus
- ?????????? ?? JUnit
- Q A
31Cactus
- JUnit ???????????? unit ???????? (functional
unit testing). - Cactus ??????????? unit ????????
(integration/in-container unit testing). - ?????????? ?? JUnit ??????????? ???????? ?? J2EE
?????????? - JSP/Servlets, TagLibs
- Filters
- EJB
- http//jakarta.apache.org/cactus/
- ?????? ?????? 1.7
- J2?? 1.2
- J2EE 1.3
32??????????? lt1gt
33??????????? lt2gt
YYY Servlet Filter JSP Server
Tomcat Jetty (embedded server ) Other
- ?????????
- beginXXX(), endXXX() ?? ?????????? ??? ???????
- setUp() ? tearDown() ?? ?????????? ??? ???????
- ????? Test Case ?????? ?? ????????? ???? ?? ????
??????? - ServletTestCase
- JSPTestCase
- FilterTestCase
34?????? lt1gt
- import org.apache.cactus.ServletTestCase
- import org.apache.cactus.WebRequest
- import org.apache.cactus.WebResponse
- public class TestCalculatorServlet extends
ServletTestCase - public void beginTestCalculator(WebRequest
request) - request.addParameter("num1", "21")
- request.addParameter("num2", "21")
-
- .
-
35?????? lt2gt
- .
- public void testTestCalculator() throws
Exception - CalculatorServlet calc new
CalculatorServlet() - calc.doPost(request, response)
-
- public void endTestCalculator(WebResponse
response) - assertEquals("result 42",
response.getText()) -
-
36??????????
- Jetty
- public static Test suite()
-
- TestSuite suite new TestSuite("Jetty
testing") - suite.addTestSuite(org.devbg.TestCalculato
rServlet.class) - System.setProperty("cactus.contextURL",
"http//localhost8999") - return new JettyTestSetup(suite)
-
- ???? Application Server
- cactus.contextURL, cactus.servletRedirectorName,
cactus.jspRedirectorName, cactus.filterRedirectorN
ame ??? ??????? - org.apache.cactus.server.FilterTestRedirector
?????? ?? ?? ?????? ???? Filter ??? ??????? - org.apache.cactus.server.ServletTestRedirector
??? ??????? - jspRedirector.jsp ??? ???????
37Test Runners
- JUnit
- Swing AWT Text
- ?????? ?? ??? ??????? ?????? ??? ?? ?? ????????
embedded Jetty. - ? Application Sever
- ?????? ?? ?? ?????? ???????? ????
- org.apache.cactus.server.runner.ServletTestRunner
- http//ltservergtltportgt/ltcontextgt/ltnamegt?suiteltte
st_casetest_suitegt
38?????????? ? Ant
- ltjunitgt
- ltcactusgt (?????????e ?? ???????? ltjunitgt)
- ltrunservertestsgt
- http//jakarta.apache.org/cactus/integration/ant/
39Demo Cactus Test Case, Runners
DEMO
40??????????
- ????????? ? JUnit
- JUnit ? Ant
- ????????? ? Cactus
- ?????????? ?? JUnit
- Q A
41?????????? ?? JUnit
- StrutsTestCase
- http//strutstestcase.sourceforge.net/
- JUnitPerf
- http//www.clarkware.com/software/JUnitPerf.html
- DbUnit
- http//dbunit.sourceforge.net/
- HttpUnit
- http//httpunit.sourceforge.net/
42???????
- http//www.junit.org
- http//junit.sourceforge.net/doc/faq/faq.htm
- http//jakarta.apache.org/cactus/
43Q A
44?????????????
- ??????? ?????
- ????? ???????
- ?????? ???????
45????