Dashboard > Struts University > Home > JPA Training Course > JPA Agenda > CRUD-Notes
CRUD-Notes
Added by Ted Husted, last edited by Ted Husted on Dec 06, 2007  (view change)
Labels: 
(None)


Why do we test applications? How do we test applications? Why do we need a testing framework? How do we get started with JUnit?

Presentation - JUnit Jumpstart

The JPA encourages a layered architecture which implies a layered approach to testing. We learn how to interact with the the business and persistence layers through unit tests.

  • Writing simple tests by hand
  • Writing better tests with JUnit
  • Installing JUnit and running tests

Workshop - CRUD

In the lab, we work up a set of standard CRUD tests for our starter object.

Story: The MailReader application will manage several objects using standard Create / Read / Update / Delete operations. We should setup a sample test suite that demonstrates the usual CRUD operations.

  • Create unit tests for persistence code
  • Learn how to use the persistence code API

Prerequisites

  • persistence.xml
  • EntityManagerHelper
  • MessageTestCase
    • setUp, tearDown, testTrue

New Dependencies

  • javax.persistence.Query;

Exercises

  • Continue using your own solution or create a new module using the "hello" solution.
  • Review the CRUD use case.
  • Extend the MessageTestCase to exercise the database methods shown in the CRUD Main Success Scenario.

Hint Code

Here's a count helper method and a test for creating (persisting) a Message entity.

public int count() throws Exception {
    final String COUNT_QUERY = "SELECT COUNT(*) FROM APP_MESSAGE";
    Query query = manager.createQuery(COUNT_QUERY);
    Long count = (Long) query.getSingleResult();
    int result = count.intValue();
    return result;
    }
    
    public void testCreate() throws Exception {
    final String MESSAGE = "Hello World!";
    
    Message message = new Message(MESSAGE);
    
    String text = message.getText();
    assertEquals(MESSAGE, text);
    
    int beginCount = count();
    manager.persist(message);
    int endCount = count();
    
    assertTrue("Expected count to increase!", endCount > beginCount);
    }
  • To commit the transaction-so-far, call manager.flush()
  • To delete a message (detach it), call manager.remove(object)
  • To test deleting, write a testCreateDelete method that cleans up after itself.

Accomplishments

  • Created unit tests for persistence code
  • Learned how to use the persistence code API

Extra Credit

  • Set breakpoints and step through the code as you write it.
  • Flip over to the Database Explorer and verify the changes to the database.
  • Create a "bootstrap" test that seeds the database with rows that can be used in other tests.

Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators