130 likes | 249 Views
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.
E N D
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
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).
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
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.
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.
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
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/
Example publicclassClassUnderTest { /** */ publicvoid service(){ Collaborator.getInstance().speak(); } /** */ publicstaticvoid main(String[] args) { ClassUnderTest main = newClassUnderTest(); main.service(); } } Ref: http://octodecillion.com/blog/aopfortest/
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/
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