Skip to Main Content
Spring: A Developer's Notebook
book

Spring: A Developer's Notebook

by Bruce Tate, Justin Gehtland
April 2005
Intermediate to advanced content levelIntermediate to advanced
216 pages
4h 44m
English
O'Reilly Media, Inc.
Content preview from Spring: A Developer's Notebook

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!

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.");
    }

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

JavaBeans Unleashed

JavaBeans Unleashed

Dr. Donald Doherty, Rick Leinecker
Beginning Spring

Beginning Spring

Mert Caliskan, Kenan Sevindik, Rod Johnson, Jürgen Höller
Hibernate Search in Action

Hibernate Search in Action

Emmanuel Bernard, John Griffin

Publisher Resources

ISBN: 0596009100Supplemental ContentErrata Page