back to blog index..

Testing those aspect creatures

May 30, 2005
I was always wondering what is this hype about testing aspects or aspect oriented systems about? Or maybe there isn't any..
Here you'll find concrete examples of my JUnit tests of my Ilybra application and PAT framework.

Test type no. 1 - test of PAT aspect itself

When applying PAT to any software I have to test if PAT works correctly. To do that I need to write tests of the aspect. And PAT is a persistence aspect, so what I need to test is if the data is really stored on the disk after some persistent operation. And it means I must check if the database I use (Prevayler) behaves correctly: stores data changes on the file system, when recovering does it read files in correct order and does it restore the data after failure.
And persistent operation is PAT's transaction invoked on business object (BO).

So here is sample code, of how do I test my PAT aspect:

public void testModifyingRootAndResetingSystem()
{
    assertEquals(0, getFilesNumberInRepo());
    TestRoot root = new TestRoot();
    assertEquals(1, getFilesNumberInRepo());
    
    ((Persisteble)root).takeSnapshot();

    assertEquals(1, getFilesNumberInRepo());
    assertEquals("000000000000000000000.snapshot", 
                 getLastFileFromRepo());

    root.newUser("user 1");
    assertEquals(1, root.getUsers().size());
    assertEquals(2, getFilesNumberInRepo());
    assertEquals("000000000000000000001.commandLog",
                 getLastFileFromRepo());


    //Emulation of database restart
    Pat.unload();

    TestRoot rootReloaded = new TestRoot();
    if (rootReloaded == root)
        throw new AssertionError("Roots' references should differ");

    assertEquals(2, getFilesNumberInRepo());
    assertEquals("000000000000000000001.commandLog",
                 getLastFileFromRepo());

    assertEquals("Should be 1 user", 1, rootReloaded.getUsers().size());
}

Test type no. 2 - testing Ilybra's business methods

It is trivial to test Ilybra application and its business methods. Due to the fact, any business class created with PAT is plain Java class without any dependency on external resources, I can just invoke the object's methods without any "mocks fun".
Below you'll find one of my JUnit tests for testing user story: "Lend book to a reader". The expected behaviour after invoking method lendBook is the copy must be located within reader's current list of lend copies and the copy itself has to have a reference to the reader.

Code of the test case - again - without any cheating it is one of my original test cases. There is no external configuration to the test case. None of aspects is applied here, no custom advice here except PAT's annotations on transaction methods (lendCopy).

Note: I test core functionality here. And this is the most unique. PAT lets you create such simple test cases for an application

public void setUp()
{
    Pat.unload();
    removeDatabaseFiles();
    world = new World();
    library = world.getLibrary();
}

public void testLendBookToReader()
{
    Book book = TestObjectFactory.addBookWithAuthor(world, "Java", "Author");
    Copy copy = TestObjectFactory.addCopy(world, book, "S.2409");
    Reader reader = TestObjectFactory.addStudent(world, "Nazar", "Tomasz");

    assertEquals(0, reader.getCopies().getSize());
    library.lendCopy(copy, reader, Dates.now(), Dates.nowPlus(14));

    assertEquals(1, reader.getCopies().getSize());
    assertEquals(reader, copy.getReader());
    assertEquals(Dates.now(), copy.getLendDate());
    assertEquals(Dates.nowPlus(14), copy.getReturnDate());
}

With those two examples above I think I've just exhausted my "AOP & testing" topic.

Have fun and take care!
Tomasz Nazar

Designed with CSS| Get Firefox!| | Opel Omega Forum|

Copyright © Tomasz Nazar 2007
Revision: $Id: blog_testing_aspects.gtml 3768 2005-07-25 23:47:33Z nthx $