JUnit junit framework Assert Test Case Your Test
- Slides: 43
Дисекция на JUnit Други класове/ интерфейси: • junit. framework. * Assert Test. Case Your Test <<interface>> * Test. Suite Test. Result Test. Listener Base. Test. Runner Text Swing AWT. . .
Основни понятия • Test Fixture • Средата в която се изпълняват junit тестове • Test case • Клас: Test. Case • Test suite • Клас: Test. Suite • Assertions • Клас: Assert • Test Runner • junit. [textui|awtui|swingui]. Test. Runner • Наследяват Base. Test. Runner
Основни методи на Test. Case • Конструктори: • public Test. Case() • public Test. Case(String name) • Методи: • protected void set. Up() • protected void tear. Down()
Основни методи на Test. Suite • Конструктори • public Test. Suite() • public Test. Suite(Class the. Class, String name) • public Test. Suite(String name) • Методи: • public void add. Test(Test test) • public void add. Test. Suite(Class test. Class)
Основни методи на Assert <1> • Методи assert. XXX(…) • public static void assert. Equals([String message, ] Type 1 expected, Type 1 actual [, Type 2 delta]) • Type 1 : : = [byte, char, double, float, int, long, short, Object, String] • public static void assert. Equals(String message, int expected, int actual) • public static void assert. Equals(String expected, String actual) • Type 2 : : = [double, float], Type 2 == Type 1. Задължително при Type 1 double или float • assert. False, assert. True, assert. Not. Null, assert. Null, assert. Not. Same, assert. Same
JUnit в действие <2> package org. devbg; import junit. framework. Test. Case; public class Test. Calculator extends Test. Case{ Calculator calc; protected void set. Up() throws Exception { System. out. println("set. Up() method"); calc = new Calculator(); } …
JUnit в действие <3> … protected void tear. Down() throws Exception { System. out. println("tear. Down() method"); } public void test. Add() { System. out. println("test. Add() method"); assert. Equals(4, calc. add(2, 2)); } public void test. Sub() { System. out. println("test. Sub() method"); assert. Equals(0, calc. sub(2, 2)); } }
Стартиране на тестове (Test Runners) • Junit предлага три начина за стартиране на тестове: • junit. textui. Test. Runner • junit. awtui. Test. Runner • junit. swingui. Test. Runner • Наследяват junit. runner. Base. Test. Runner
Demo – Test Case, Suite, Runner DEMO
Keep the bar green…
JUnit и Ant • За да работи Ant с junit трябва junit. jar да е в директорията libs на Аnt. • <junit> task • <junitreport> task
<junit> task <junit haltonfailure=“no“ haltonerror=“no"> <junit <classpath> <pathelement location="${dist}"/> </classpath> <formatter type="xml"/> < ……. </junit> Classpath за изпълнението на тестовете
<test> и <batchtest> <junit> …… <test fork="yes" name="Test. All" todir="${junit. report}"/> <batchtest fork="yes" todir="${junit. report}"> <fileset dir="${test}"> <include name="**/*Test*. java"/> <exclude name="**/Test. All*. java"/> </fileset> </batchtest> “yes” – стартира нова инстанция на JVM за всеки тест метод “no” – изпълнява тестовете в текущото JVM …. </junit> Къде да търси тестовете
<junitreport> task <junitreport todir="${junit. report}"> <fileset dir="${junit. report}"> <include name="TEST-*. xml"/> </fileset> <report format="frames“ todir="${junit. report}/html"/> </junitreport> • <report> поддържа потребителски XSL трансформации. Атрибут styledir. • junit-frames. xsl. • junit-noframes. xsl • Пример: <report format=“frames” styledir=“c: /xslt/”>
JUnit & Ant DEMO
Cactus • JUnit – функционално unit тестване (functional unit testing). • Cactus – интегрирано unit тестване (integration/incontainer unit testing). • Надстройка на JUnit позволяваща тестване на J 2 EE компоненти: • JSP/Servlets, Tag. Libs • Filters • EJB • http: //jakarta. apache. org/cactus/ • Текуща версия: 1. 7 • J 2 ЕЕ 1. 2 • J 2 EE 1. 3
Пример <1> import org. apache. cactus. Servlet. Test. Case; import org. apache. cactus. Web. Request; import org. apache. cactus. Web. Response; public class Test. Calculator. Servlet extends Servlet. Test. Case { public void begin. Test. Calculator(Web. Request request) { request. add. Parameter("num 1", "21"); request. add. Parameter("num 2", "21"); } …….
Пример <2> …. public void test. Test. Calculator() throws Exception { Calculator. Servlet calc = new Calculator. Servlet(); calc. do. Post(request, response); } public void end. Test. Calculator(Web. Response response) { assert. Equals("result: 42", response. get. Text()); } }
Внедряване • Jetty public static Test suite() { Test. Suite suite = new Test. Suite("Jetty testing"); suite. add. Test. Suite(org. devbg. Test. Calculator. Servlet. class); System. set. Property("cactus. context. URL", "http: //localhost: 8999"); return new Jetty. Test. Setup(suite); } • Друг Application Server: • cactus. context. URL, cactus. servlet. Redirector. Name, cactus. jsp. Redirector. Name, cactus. filter. Redirector. Name при клиента , • org. apache. cactus. server. Filter. Test. Redirector трябва да се внедри като Filter при сървъра • org. apache. cactus. server. Servlet. Test. Redirector при сървъра • jsp. Redirector. jsp при сървъра
Test Runners • JUnit • Swing | AWT | Text • Трябва да има работещ сървър ако не се използва embedded Jetty. • В Application Sever • Трябва да се внедри следният клас: • org. apache. cactus. server. runner. Servlet. Test. Runner http: //<server>: <port>/<context>/<name>? suite=<test_case|test_suite>
Интеграция с Ant • <junit> • <cactus> (разширениe на задачата <junit>) • <runservertests> http: //jakarta. apache. org/cactus/integration/ant/
Demo – Cactus Test Case, Runners DEMO
Разширения на JUnit • Struts. Test. Case • http: //strutstestcase. sourceforge. net/ • JUnit. Perf • http: //www. clarkware. com/software/JUnit. Perf. html • Db. Unit • http: //dbunit. sourceforge. net/ • Http. Unit • http: //httpunit. sourceforge. net/
Ресурси • http: //www. junit. org • http: //junit. sourceforge. net/doc/faq. htm • http: //jakarta. apache. org/cactus/
Q&A Q & A
- Test case пример
- Restate your position
- Fixture junit
- Junit testing private methods
- Junit test concurrency
- Best case worst case average case
- Today nanotechnology produced computer transistors
- Assert vs verify
- To assert without proof or confirmation
- Knowledge base prolog
- What is assert
- Assert
- Craig wiener md
- Java assert syntax
- Rapid json github
- Java unit test
- Thecoadletter
- Junit tutorial eclipse
- Junit
- Junit testing
- Junit exercises
- Tdd junit
- Junit before after
- Give us your hungry your tired your poor
- Dispositional framework vs regulatory framework
- Conceptual and theoretical framework example
- Conceptual research examples
- Theoretical framework
- Dispositional framework vs regulatory framework
- Theoretical framework
- Business case framework
- Case study on enterprise architecture and integration
- Long case vs short case
- Worst case of binary search
- Case western reserve university case school of engineering
- Bubble sort best case and worst case
- Crm failure case study
- Bubble sort best case and worst case
- Bubble sort best case and worst case
- Ambiguous case trig
- Your garden gloves
- Open your pencil case
- Fit testing framework
- C++ unit test framework