JUG Roma 26 th  September 2006 “ Embrace Unit Testing” Alessio Pace  alessio.pace [AT] gmail.com http://www.jroller.com/pa...
3 things about me.. <ul><li>Master Degree in Computer Engineering </li></ul><ul><li>Java fan </li></ul><ul><li>Just “Pucci...
About this presentation <ul><li>What you  will  find: </li></ul><ul><ul><li>definition and survey of the benefits of Unit ...
Agenda <ul><li>Introduction: </li></ul><ul><ul><li>The programmer life without  Unit Testing </li></ul></ul><ul><ul><li>Wh...
INTRODUCTION
Programmer life  without  Unit Testing <ul><li>Write  a lot of  code...and someday something doesn't work / stops working ...
What is Unit Testing <ul><li>A  unit test  is an  automated and self-checked  procedure used to validate that a particular...
“A set of Unit Testing rules” <ul><li>A test is  not  a unit test if: </li></ul><ul><ul><li>It talks to the  database </li...
The benefits of Unit Testing (1) <ul><li>Isolate each part of the program and check that the individual parts work </li></...
The benefits of Unit Testing (2) <ul><li>Simplifies  integration : </li></ul><ul><ul><li>testing the parts of a program fi...
The benefits of Unit Testing (3) <ul><li>Separation of interface from implementation : </li></ul><ul><ul><li>a class may h...
Bad rumours about Unit Testing <ul><li>“ It requires time, and I am always overscheduled” </li></ul><ul><li>“ It is a wast...
Answers to the bad rumours (1/2) <ul><li>The  “I'm in hurry”  issue leads to a vicious cycle: </li></ul><ul><ul><li>more p...
Answers to the bad rumours (2/2) <ul><li>Unit Testing  speeds up your development :  </li></ul><ul><ul><li>automated and s...
A short parenthesis on Refactoring <ul><li>Refactoring , a definition: </li></ul><ul><ul><li>“ A change made to the intern...
PRACTICAL EXAMPLE
Let's get into a coding example! <ul><li>A practical example showing the very basics of: </li></ul><ul><ul><li>JUnit 4.x +...
Our example:  a high level description <ul><li>Suppose we are  already  given a  IBookService   interface, which defines a...
Our example:  UML Class Diagram We have to  implement  and  test  the  CachedBookService  class
Constructor implementation of  CachedBookService public class  CachedBookService  implements IBookService { private IBookS...
How to automatically test the constructor? <ul><li>We must check that: </li></ul><ul><ul><li>when passing a valid argument...
JUnit: description <ul><li>JUnit  is just a general testing framework which provides  assertion methods </li></ul><ul><li>...
Writing a JUnit 4.x test class  <ul><li>A test class is just a Java class </li></ul><ul><li>Test methods must be annotated...
Testing object state after constructor import static org.junit.Assert.*; import org.junit.Test; public class  CachedBookSe...
Testing exception is thrown //  CachedBookServiceTest.java @Test(expected=IllegalArgumentException.class) public void  tes...
Enjoy the green bar :-)
Now let's implement  getBook()  method public class CachedBookService implements IBookService { private IbookService remot...
How to test  getBook()? <ul><li>The class must be tested in isolation: </li></ul><ul><ul><li>we don't want to rely on a co...
What are Mock Objects? <ul><li>Most software units do not work alone, but collaborate with other units </li></ul><ul><li>T...
Why are Mocks useful? <ul><li>Isolate bugs </li></ul><ul><li>Depend on an interface and not on an implementation (which co...
EasyMock 2.2 <ul><li>EasyMock 2.2, two libraries: </li></ul><ul><ul><li>one to generate mocks for interfaces only ( easymo...
How to get a mock object with EasyMock 2.2 <ul><li>create  a Mock Object for the interface  to simulate </li></ul><ul><li>...
Testing getBook() - 1 st  test method @Test public void  testGetBook () { // (1) create the mock for the collaborating obj...
Testing getBook() - 2 nd  test method @Test public void  testGetBookTwoDifferentCodes () { // (1) create the mock for the ...
Testing getBook() - 3 rd  test method @Test public void  testGetSameBookTwoTimes () { IBookService mockBookService = EasyM...
Adding an IBookXmlSerializer <ul><li>We are already given an  IBookXmlSerializer  interface to serialize an IBook to XML <...
IBookXmlSerializer
IBookXmlSerializerImpl public class  IBookXmlSerializerImpl  implements IBookXmlSerializer { public String serialize(IBook...
How to test serialize()? <ul><li>We must check that the XML output is correct </li></ul><ul><li>We must test the class in ...
Testing XML with XMLUnit <ul><li>“ XMLUnit extends JUnit to simplify unit testing of XML” </li></ul><ul><li>“ It compares ...
IbookXmlSerializerTest - Part 1 import org.custommonkey.xmlunit.XMLTestCase; public class IBookXmlSerializerImplTest { pri...
IbookXmlSerializerTest - Part 2 @Test public void testSerialize() throws Exception { final String code = &quot;id1&quot;; ...
If you appreciated Unit Testing... <ul><li>...you will want to try also: </li></ul><ul><ul><li>Test Driven Development (TD...
Test coverage with Cobertura
<ul><ul><li>Open discussion </li></ul></ul>
Some references 1/2 <ul><li>Wikipedia:   </li></ul><ul><ul><li>http://en.wikipedia.org/wiki/Unit_testing </li></ul></ul><u...
Some references 2/2 <ul><li>“ Endo-Testing: Unit Testing with Mock Objects”: </li></ul><ul><ul><li>www.connextra.com/about...
Upcoming SlideShare
Loading in...5
×

Embrace Unit Testing

12,413

Published on

The presentation contains a definition and survey of the benefits of Unit Testing, and a little coding example to get the feeling of Unit Testing using JUnit, EasyMock and XMLUnit.

Published in: Technology, Education
6 Comments
54 Likes
Statistics
Notes
  • very nice one, can you please explain how we can achieve the same when we need to deal with complex UI (Eclipse SWT/JFACE)...
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • good ppt
    if possible
    please mail
    ksubbaraju.tm@gmail.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • As a management instructor I appreciate viewing the function of others. This is among the best demonstration on planning I have viewed.
    Sharika
    http://financeadded.com http://traveltreble.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • good ppt...appreciate if you can send it to met meera_visa@yahoo.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • this is very nice
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
12,413
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
0
Comments
6
Likes
54
Embeds 0
No embeds

No notes for slide

Transcript of "Embrace Unit Testing"

  1. 1. JUG Roma 26 th September 2006 “ Embrace Unit Testing” Alessio Pace alessio.pace [AT] gmail.com http://www.jroller.com/page/alessiopace
  2. 2. 3 things about me.. <ul><li>Master Degree in Computer Engineering </li></ul><ul><li>Java fan </li></ul><ul><li>Just “Puccio” for the friends </li></ul>
  3. 3. About this presentation <ul><li>What you will find: </li></ul><ul><ul><li>definition and survey of the benefits of Unit Testing </li></ul></ul><ul><ul><ul><li>reusing material from the Web and books (see references at the end) </li></ul></ul></ul><ul><ul><li>a little coding example to get the feeling of Unit Testing using JUnit, EasyMock and XMLUnit </li></ul></ul><ul><li>What you won't find: </li></ul><ul><ul><li>an in-depth explanation about the internals of the above testing frameworks </li></ul></ul><ul><ul><li>a Test Driven example </li></ul></ul>
  4. 4. Agenda <ul><li>Introduction: </li></ul><ul><ul><li>The programmer life without Unit Testing </li></ul></ul><ul><ul><li>What is and what is not Unit Testing </li></ul></ul><ul><ul><li>The benefits of Unit Testing </li></ul></ul><ul><ul><li>Bad rumours about Unit Testing, and the answers to them </li></ul></ul><ul><li>Practical example in Java: </li></ul><ul><ul><li>JUnit + EasyMock </li></ul></ul><ul><ul><li>JUnit + EasyMock + XMLUnit </li></ul></ul>
  5. 5. INTRODUCTION
  6. 6. Programmer life without Unit Testing <ul><li>Write a lot of code...and someday something doesn't work / stops working </li></ul><ul><li>Just fixing a bug is easy, but finding it can be a nightmare: </li></ul><ul><ul><li>hours in front of the debugger </li></ul></ul><ul><ul><li>interact like a monkey with the browser or reading the output on console </li></ul></ul><ul><li>Fixing it could break other parts </li></ul><ul><li>The bug could appear again later </li></ul>
  7. 7. What is Unit Testing <ul><li>A unit test is an automated and self-checked procedure used to validate that a particular module of source code works correctly </li></ul><ul><ul><li>a module: a single Java class </li></ul></ul><ul><li>The procedure is to write tests for all the methods or functions of the module </li></ul><ul><li>The class under test should be tested in isolation </li></ul>
  8. 8. “A set of Unit Testing rules” <ul><li>A test is not a unit test if: </li></ul><ul><ul><li>It talks to the database </li></ul></ul><ul><ul><li>It communicates across the network </li></ul></ul><ul><ul><li>It touches the file system </li></ul></ul><ul><ul><li>It can't run at the same time as any of your other unit tests </li></ul></ul><ul><ul><li>You have to do special things to your environment (such as editing config files) to run it </li></ul></ul><ul><ul><li>You are not testing a class in isolation from other concrete classes </li></ul></ul>
  9. 9. The benefits of Unit Testing (1) <ul><li>Isolate each part of the program and check that the individual parts work </li></ul><ul><li>Strict, written contract that the piece of code must satisfy </li></ul><ul><li>Allows to refactor code and make sure it still works correctly ( regression testing ) </li></ul>
  10. 10. The benefits of Unit Testing (2) <ul><li>Simplifies integration : </li></ul><ul><ul><li>testing the parts of a program first and then testing their integration </li></ul></ul><ul><li>Provides documentation : </li></ul><ul><ul><li>clients and other developers can look at the unit tests to gain a basic understanding of the APIs and determine how to use the module to fit their needs </li></ul></ul><ul><li>Improves code quality : </li></ul><ul><ul><li>code that cannot be easily tested is not factored properly </li></ul></ul>
  11. 11. The benefits of Unit Testing (3) <ul><li>Separation of interface from implementation : </li></ul><ul><ul><li>a class may have some collaborator classes, testing that class can frequently lead into testing the other classes.. </li></ul></ul><ul><ul><li>unit test should never go outside of its own class boundary: </li></ul></ul><ul><ul><ul><li>abstract an interface around the collaborator class, and implements that interface with a mock object (see later). This results in loosely coupled code, minimizing dependencies in the system </li></ul></ul></ul>
  12. 12. Bad rumours about Unit Testing <ul><li>“ It requires time, and I am always overscheduled” </li></ul><ul><li>“ It is a waste of time: if I change my classes, I have to rewrite the tests!” </li></ul><ul><li>“ My code is rock solid, I don't need tests!” </li></ul>
  13. 13. Answers to the bad rumours (1/2) <ul><li>The “I'm in hurry” issue leads to a vicious cycle: </li></ul><ul><ul><li>more pressure you feel -> less tests you write </li></ul></ul><ul><ul><li>less tests you write -> less productive you are and the less stable your code becomes </li></ul></ul><ul><ul><li>the less productive and accurate you are -> more pressure you feel </li></ul></ul>
  14. 14. Answers to the bad rumours (2/2) <ul><li>Unit Testing speeds up your development : </li></ul><ul><ul><li>automated and self-checked code reduces the time to manually debug your code </li></ul></ul><ul><ul><li>writing unit tests immediately after you code new features (or even better, before ) lets you find bugs sooner and isolated (easy to correct) </li></ul></ul><ul><li>Unit Testing lets you refactor safely: </li></ul><ul><ul><li>a change in code that causes a regression is immediately found </li></ul></ul>
  15. 15. A short parenthesis on Refactoring <ul><li>Refactoring , a definition: </li></ul><ul><ul><li>“ A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behaviour” (“Refactoring”, Martin Fowler) </li></ul></ul><ul><li>Unit Testing and Refactoring go together: </li></ul><ul><ul><li>before refactoring a section of code you better have solid tests that cover it </li></ul></ul><ul><ul><li>test, small change, test, small change, test, .. </li></ul></ul>
  16. 16. PRACTICAL EXAMPLE
  17. 17. Let's get into a coding example! <ul><li>A practical example showing the very basics of: </li></ul><ul><ul><li>JUnit 4.x + EasyMock 2.2 </li></ul></ul><ul><ul><li>JUnit 4.x + EasyMock 2.2 + XMLUnit </li></ul></ul><ul><li>See the References at the end for more tutorials and explanations about the internals of the frameworks </li></ul>
  18. 18. Our example: a high level description <ul><li>Suppose we are already given a IBookService interface, which defines a method to retrieve an IBook by its code </li></ul><ul><li>Suppose also we are already given an implementation of it: RemoteBookService </li></ul><ul><li>We want to implement and test: </li></ul><ul><ul><li>a trivial CachedIBookService implementation, which decorates an IBookService by caching subsequent calls for the same book code value </li></ul></ul>
  19. 19. Our example: UML Class Diagram We have to implement and test the CachedBookService class
  20. 20. Constructor implementation of CachedBookService public class CachedBookService implements IBookService { private IBookService remoteBookService ; public CachedBookService(IBookService bookService){ if(bookService == null){ throw new IllegalArgumentException(); } this.remoteBookService = bookService; } public IBookService getRemoteBookService() { return remoteBookService; } public IBook getBook(String code) throws NoSuchElementException { // implementation left out for the moment throw new NotImplementedException(); } } Ok, let's test the constructor
  21. 21. How to automatically test the constructor? <ul><li>We must check that: </li></ul><ul><ul><li>when passing a valid argument the object state after the construction is correct (the instance variable remoteBookService is set) </li></ul></ul><ul><ul><li>when passing a null argument an IllegalArgumentException is thrown </li></ul></ul><ul><li>We will use a testing framework: JUnit </li></ul>
  22. 22. JUnit: description <ul><li>JUnit is just a general testing framework which provides assertion methods </li></ul><ul><li>You can use it to do unit testing : </li></ul><ul><ul><li>testing a single class in isolation, eventually using EasyMock (or other mock objects frameworks) to mock collaborators classes </li></ul></ul><ul><li>You can use it to do integration testing : </li></ul><ul><ul><li>testing the interactions of a class with real collaborator classes </li></ul></ul><ul><li>Almost a de-facto standard in the Java field (but have a look also at TestNG ..) </li></ul>
  23. 23. Writing a JUnit 4.x test class <ul><li>A test class is just a Java class </li></ul><ul><li>Test methods must be annotated with @Test </li></ul><ul><li>Two configuration levels: </li></ul><ul><ul><li>before/after each test method call ( @Before / @After annotated methods) </li></ul></ul><ul><ul><li>before/after the test class is processed ( @BeforeClass / @AfterClass annotated methods) </li></ul></ul><ul><li>Launch the test class from an IDE (“run as JUnit Test”) or the command line </li></ul>
  24. 24. Testing object state after constructor import static org.junit.Assert.*; import org.junit.Test; public class CachedBookServiceTest { @Test public void testConstructor () { // anonymous class to test in isolation CachedBookService constructor IBookService bookServiceParameter = new IBookService(){ public IBook getBook(String code) throws NoSuchElementException { return null; } }; CachedBookService cachedBookService = new CachedBookService(bookServiceParameter); assertNotNull (&quot;Assert not null&quot;, cachedBookService.getRemoteBookService()); /* 3 identical ways to test the reference value: */ assertTrue (&quot;Assert same reference&quot;, bookServiceParameter == cachedBookService.getRemoteBookService()); // assertSame (&quot;Assert same reference&quot;, bookServiceParameter, cachedBookService.getRemoteBookService()); // assertEquals (“Assert same reference”, bookServiceParameter, cachedBookService.getRemoteBookService()) }
  25. 25. Testing exception is thrown // CachedBookServiceTest.java @Test(expected=IllegalArgumentException.class) public void testConstructorWithNullArg () { // should throw IllegaArgumentException with null arg new CachedBookService( null ); } }
  26. 26. Enjoy the green bar :-)
  27. 27. Now let's implement getBook() method public class CachedBookService implements IBookService { private IbookService remoteBookService; private Map<String, IBook> cache; public CachedBookService(IBookService bookService){ if(bookService == null){ throw new IllegalArgumentException(); } this.remoteBookService = bookService; this.cache = new HashMap<String, IBook>(); } public IBook getBook(String code) throws NoSuchElementException { IBook ibook = cache.get(code); if(ibook != null){ return ibook; }else{ IBook result = this.remoteBookService.getBook(code); this.cache.put(code, result); return result; } } Ok, let's test getBook()
  28. 28. How to test getBook()? <ul><li>The class must be tested in isolation: </li></ul><ul><ul><li>we don't want to rely on a concrete IBookService collaborator object </li></ul></ul><ul><li>We must test the interactions of the class under test with the collaborator object: </li></ul><ul><ul><li>first request for an IBook code must be delegated to the remoteBookService </li></ul></ul><ul><ul><li>following requests for the same IBook code must be served from the cache </li></ul></ul><ul><li>We are going to use JUnit with a Mock Objects framework: EasyMock </li></ul>
  29. 29. What are Mock Objects? <ul><li>Most software units do not work alone, but collaborate with other units </li></ul><ul><li>To test a unit in isolation , the collaborators must be simulated </li></ul><ul><li>Mock objects have the same interfaces of the objects they mimic </li></ul><ul><ul><li>a mock is configured to simulate the object behaviour that it replaces </li></ul></ul><ul><ul><li>In contrast to a stub , a mock object also verifies whether it is used as expected </li></ul></ul>
  30. 30. Why are Mocks useful? <ul><li>Isolate bugs </li></ul><ul><li>Depend on an interface and not on an implementation (which couldn't exist yet) </li></ul><ul><li>Endo testing : </li></ul><ul><ul><li>not just testing the output of a method, but testing the interactions of a unit with the collaborator object(s) </li></ul></ul><ul><ul><li>also called “interaction based testing” (as opposed to “state based testing” ) </li></ul></ul>
  31. 31. EasyMock 2.2 <ul><li>EasyMock 2.2, two libraries: </li></ul><ul><ul><li>one to generate mocks for interfaces only ( easymock ) </li></ul></ul><ul><ul><li>one to generate mocks for non-final concrete classes and interfaces ( easymock-classextension ) </li></ul></ul><ul><li>Benefits: </li></ul><ul><ul><li>hand writing mocks is not needed </li></ul></ul><ul><ul><li>supports compiler check and refactoring </li></ul></ul><ul><ul><li>supports method return values and exceptions </li></ul></ul><ul><ul><li>supports checking the order and count of method calls </li></ul></ul><ul><ul><li>... </li></ul></ul>
  32. 32. How to get a mock object with EasyMock 2.2 <ul><li>create a Mock Object for the interface to simulate </li></ul><ul><li>record the expected behavior </li></ul><ul><li>switch the mock object to replay state </li></ul><ul><li>perform the method call to the class under test that is supposed to interact with the mock obejct </li></ul><ul><li>verify the mock has been used correctly </li></ul>
  33. 33. Testing getBook() - 1 st test method @Test public void testGetBook () { // (1) create the mock for the collaborating object IBookService mockBookService = EasyMock.createMock(IBookService.class); // a mock with no behaviour as return value of getBook() IBook mockBook = EasyMock.createMock(IBook.class); // (2) record the expected behaviour EasyMock.expect(mockBookService.getBook(&quot;id123&quot;)).andReturn(mockBook); // (3) switch the mock to replay state EasyMock.replay(mockBookService); CachedBookService cachedBookService = new CachedBookService(mockBookService); // (4) perform the call to the class under test which interacts with the mock IBook result = cachedBookService.getBook(&quot;id123&quot;); assertSame(“Assert same reference”, mockBook, result); // (5) verify the mock has been used as expected EasyMock.verify(mockBookService); }
  34. 34. Testing getBook() - 2 nd test method @Test public void testGetBookTwoDifferentCodes () { // (1) create the mock for the collaborating object IBookService mockBookService = EasyMock.createMock(IBookService.class); // a mock with no behaviour as return value of getBook() IBook mockBook = EasyMock.createMock(IBook.class); IBook mockBook2 = EasyMock.createMock(IBook.class); // (2) record two expected calls EasyMock.expect(mockBookService.getBook(&quot;id123&quot;)).andReturn(mockBook); EasyMock.expect(mockBookService.getBook(&quot;id456&quot;)).andReturn(mockBook2); // (3) switch the mock to replay state EasyMock.replay(mockBookService); CachedBookService cachedBookService = new CachedBookService(mockBookService); // (4) perform the call to the class under test which interacts with the mock IBook result = cachedBookService.getBook(&quot;id123&quot;); assertSame(“Assert same reference”, mockBook, result); IBook result2 = cachedBookService.getBook(&quot;id456&quot;); assertSame(“Assert same reference”, mockBook2, result2); // (5) verify the mock has been used as expected EasyMock.verify(mockBookService); }
  35. 35. Testing getBook() - 3 rd test method @Test public void testGetSameBookTwoTimes () { IBookService mockBookService = EasyMock.createMock(IBookService.class); IBook mockBook = EasyMock.createMock(IBook.class); // record only one call EasyMock.expect(mockBookService.getBook(&quot;id123&quot;)).andReturn(mockBook); EasyMock.replay(mockBookService); CachedBookService cachedBookService = new CachedBookService(mockBookService); IBook result = cachedBookService.getBook(&quot;id123&quot;); assertSame(mockBook, result); IBook secondResult = cachedBookService.getBook(&quot;id123&quot;); assertSame(result, secondResult); EasyMock.verify(mockBookService); }
  36. 36. Adding an IBookXmlSerializer <ul><li>We are already given an IBookXmlSerializer interface to serialize an IBook to XML </li></ul><ul><li>We have to implement and test a simple IBookXmlSerializerImpl which writes an XML representation of an IBook </li></ul>
  37. 37. IBookXmlSerializer
  38. 38. IBookXmlSerializerImpl public class IBookXmlSerializerImpl implements IBookXmlSerializer { public String serialize(IBook book) { StringBuilder sb = new StringBuilder(&quot;&quot;); sb.append(&quot;<book>&quot;); sb.append(&quot; <code>&quot; + book.getCode() + &quot;</code>&quot;); sb.append(&quot; <title>&quot; + book.getTitle() + &quot;</title>&quot;); sb.append(&quot; <author>&quot; + book.getAuthor() + &quot;</author>&quot;); sb.append(&quot;</book>&quot;); return sb.toString(); } } Ok, let's test serialize()
  39. 39. How to test serialize()? <ul><li>We must check that the XML output is correct </li></ul><ul><li>We must test the class in isolation: </li></ul><ul><ul><li>we can't rely on a concrete IBook implementation (we don't have it..) </li></ul></ul><ul><li>We are going to use JUnit, EasyMock and XMLUnit </li></ul>
  40. 40. Testing XML with XMLUnit <ul><li>“ XMLUnit extends JUnit to simplify unit testing of XML” </li></ul><ul><li>“ It compares a control XML document to a test document or the result of a transformation, validates documents against a DTD, and compares the results of XPath expressions” </li></ul>
  41. 41. IbookXmlSerializerTest - Part 1 import org.custommonkey.xmlunit.XMLTestCase; public class IBookXmlSerializerImplTest { private IBookXmlSerializer serializer; private IBook book; private XMLTestCase xmlTestCase; @Before public void setUp() throws Exception { this.serializer = new IBookXmlSerializerImpl(); this.book = EasyMock.createMock(IBook.class); this.xmlTestCase = new XMLTestCase(); } // .... The method annotated with @Before is executed before every @Test method execution
  42. 42. IbookXmlSerializerTest - Part 2 @Test public void testSerialize() throws Exception { final String code = &quot;id1&quot;; EasyMock.expect(this.book.getCode()).andReturn(code); final String title = &quot;Unit Testing&quot;; EasyMock.expect(this.book.getTitle()).andReturn(title); final String author = &quot;Mario Rossi&quot;; EasyMock.expect(this.book.getAuthor()).andReturn(author); // switch to replay state EasyMock.replay(this.book); String xml = this.serializer.serialize(book); this.xmlTestCase.assertXpathExists(&quot;/book&quot;, xml); this.xmlTestCase.assertXpathEvaluatesTo(code, &quot;/book/code&quot;, xml); this.xmlTestCase.assertXpathEvaluatesTo(title, &quot;/book/title&quot;, xml); this.xmlTestCase.assertXpathEvaluatesTo(author, &quot;/book/author&quot;, xml); // verify mock EasyMock.verify(this.book); }
  43. 43. If you appreciated Unit Testing... <ul><li>...you will want to try also: </li></ul><ul><ul><li>Test Driven Development (TDD) </li></ul></ul><ul><ul><li>Continous Integration </li></ul></ul><ul><ul><li>Test Coverage tools </li></ul></ul><ul><ul><li>.. </li></ul></ul>
  44. 44. Test coverage with Cobertura
  45. 45. <ul><ul><li>Open discussion </li></ul></ul>
  46. 46. Some references 1/2 <ul><li>Wikipedia: </li></ul><ul><ul><li>http://en.wikipedia.org/wiki/Unit_testing </li></ul></ul><ul><ul><li>http://en.wikipedia.org/wiki/Mock_object </li></ul></ul><ul><li>“ A Set of Unit Testing Rules” by Michael Feathers: </li></ul><ul><ul><li>http://www.artima.com/weblogs/viewpost.jsp?thread=126923 </li></ul></ul><ul><li>“ Extreme Programing Explained / Installed” by Kent Beck / Ron Jeffries </li></ul><ul><li>“ Refactoring” by Martin Fowler </li></ul><ul><li>“ JUnit Test Infected: Programmers Love Writing Tests”: </li></ul><ul><ul><li>http://junit.sourceforge.net/doc/testinfected/testing.htm </li></ul></ul><ul><li>JUnit 4.x Cookbok: </li></ul><ul><ul><li>http://junit.sourceforge.net/doc/cookbook/cookbook.htm </li></ul></ul>
  47. 47. Some references 2/2 <ul><li>“ Endo-Testing: Unit Testing with Mock Objects”: </li></ul><ul><ul><li>www.connextra.com/aboutUs/mockobjects.pdf </li></ul></ul><ul><li>EasyMock 2.2 Documentation: </li></ul><ul><ul><li>http://easymock.org/EasyMock2_2_Documentation.html </li></ul></ul><ul><ul><li>http://easymock.org/EasyMock2_2_ClassExtension _Documentation.html </li></ul></ul><ul><li>“ Partial Mocks with EasyMock”: </li></ul><ul><ul><li>http://www.jroller.com/page/alessiopace?entry=partial_mocks_with_easymock </li></ul></ul><ul><li>“ Making more objects mockable with ASM Definalizer” </li></ul><ul><ul><li>http://sixlegs.com/blog/java/definalizer.html </li></ul></ul><ul><li>XMLUnit: </li></ul><ul><ul><li>http://xmlunit.sourceforge.net/ </li></ul></ul>

×