1 / 13

Testing with Aspect-Oriented Mock Objects

Testing with Aspect-Oriented Mock Objects. Thirumalai Samy Rajkumar. Unit Testing. Make sure code works properly. Unit testing – test one feature at a time . Fast and simple. Test cases should be independent of other. What if one feature is dependent on other? stubs , mock objects.

mae
Download Presentation

Testing with Aspect-Oriented Mock Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Testing with Aspect-Oriented Mock Objects Thirumalai Samy Rajkumar

  2. Unit Testing • Make sure code works properly. • Unit testing – test one feature at a time. • Fast and simple. • Test cases should be independent of other. • What if one feature is dependent on other? • stubs, mock objects

  3. Mock Objects • Special case objects that mimic real objects for testing. • Generally four phases • Setup, exercise, verify, teardown. • How it is different? • Behavior Verification. • Advantages • Easy to setup and use, Fast, Can create state that is difficult to produce (network error).

  4. Mock Object • public class OrderInteractionTester extends MockObjectTestCase { • public void testFillingRemovesInventoryIfInStock() { • //setup - data • Order order = new Order(“tablet”, 50); • Mock warehouseMock = new Mock(Warehouse.class); • //setup - expectations • warehouseMock.expects(once()).method("hasInventory") .with(eq(“tablet”),eq(50)).will(returnValue(true)); • warehouseMock.expects(once()).method("remove").with(eq(“tablet”), eq(50)).after("hasInventory"); • //exercise • order.fill((Warehouse) warehouseMock.proxy()); • //verify • warehouseMock.verify(); • assertTrue(order.isFilled()); • } • }Ref: http://www.martinfowler.com/articles/mocksArentStubs.html

  5. Mocks - Limitations • Mocks replace the original behavior of the code . • Hard to mock private, static methods. • Code refactoring may be required. • Consider testing right from design. • Overuse of mocks can lead to tests that don't really test anything.

  6. Aspects as Mocks • Aspects are supersets of mocks. • JoinPoint Model • Define Pointcuts to identify the tests. • Use advice to work around the original code and return mock objects.

  7. Demonstration

  8. Advantages • Flexibility • Can use a single pointcut instead of using multiple mocks objects.(looking up DB to retrieve data). • Can be used in situations that would usually prohibit the use of mock objects

  9. Example publicclass Collaborator { privatestaticfinal Collaborator instance = new Collaborator(); /** */ private Collaborator(){ } /** */ publicstatic Collaborator getInstance(){ returninstance; } /** */ publicvoid speak(){ System.out.println("Hello!"); } } Ref: http://octodecillion.com/blog/aopfortest/

  10. Example publicclassClassUnderTest { /** */ publicvoid service(){ Collaborator.getInstance().speak(); } /** */ publicstaticvoid main(String[] args) { ClassUnderTest main = newClassUnderTest(); main.service(); } } Ref: http://octodecillion.com/blog/aopfortest/

  11. Example publicaspectTestAspect { pointcut cut() : execution(publicvoidClassUnderTest.service()) && !within(TestAspect); pointcut service() : cflow(cut()) && call(publicvoidCollaborator.speak()); voidaround() : service() { System.out.println("Goodbye"); } } Ref: http://octodecillion.com/blog/aopfortest/

  12. Aspects - Testing • Aspects – depends on contexts of other class. • Tightly coupled with the class . • Many possible sources for a fault • Incorrect strength in pointcutpatterns • Incorrect aspect precedence • Failure to establish expected post conditions • Failure to preserve state invariants • Incorrect changes in control dependencies

  13. Questions?

More Related