Section 6.6. Testing a Service with Side Effects

team bbl


6.6. Testing a Service with Side Effects

Of course, you didn't learn anything about the way our code used the services. To test the services, you'll need to attach them to real code and exercise the code. In your test, you can merely look for a side effect.

When you use AOP, the business objects don't always get exercised with basic out-of-container tests. You've got to run some tests in the container to make sure that transactions get committed or rolled back as they should, and that the security is behaving appropriately. Remember, the context is part of the program!

6.6.1. How do I do that?

You'll simply exercise some code that's using an AOP service and you'll look for a known side-effect. Example 6-19 is a test case that causes an exception in the façade. We assert that the changes did not get committed.

Example 6-19. ControllerTest.java
public void testAddBadBike( ) throws Exception {         int origCount = store.getBikes( ).size( );         // collision on uniquely indexed serial number 11111         Bike bike = new Bike(-1, "MyMan", "MyBike",             12, "11111", 12.00, "New");         try {             store.saveBike(bike);         } catch (Exception ex) {             assertEquals(origCount, store.getBikes( ).size( ));             return;         }         fail("Should have errored out on bad insert.");     }

6.6.2. What just happened?

You once again fired a test case from within the container. The test case called our façade directly. The façade did a database insert, but the bad data forced the transaction to roll back. Your test case made sure that the data did not get committed by doing a second read. In the next chapter, you'll release the full power of AOP using some of Spring's pre-built interceptors.

If you've never seen AOP or interceptors before, this chapter might have been challenging for you. If you want to learn more about aspect-oriented programming or design patterns for proxying interfaces, turn to these resources:

  • AspectJ in Action, by Ramnivas Laddad (Manning)

  • Mastering AspectJ: Aspect-Oriented Programming in Java, by Joseph D. Gradecki and Nicholas Lesiecki (Wiley)

  • "Take control with the Proxy design pattern," by David Geary (http://www.javaworld.com/javaworld/jw-02-2002/jw-0222-designpatterns.html)

    team bbl



    Spring. A developer's Notebook
    Spring: A Developers Notebook
    ISBN: 0596009100
    EAN: 2147483647
    Year: 2005
    Pages: 90

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net