Class TestDatabase

java.lang.Object
petclinic.util.TestDatabase
Direct Known Subclasses:
OwnerData, PetData, VetData, VisitData

public class TestDatabase extends Object
Base test utility class for domain-specific "EntityXData" classes, which allow each test to create the required data in the test database.

Ensures that tests execute in a database transaction which is always rolled back at the end.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Database
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    assertCreated(BaseEntity newEntity, String qlStatement, Object... qlArgs)
    Verifies that a given entity was properly inserted into the database, including the generation of its integer identifier.
    final <E extends BaseEntity>
    E
    findOne(String qlStatement, Object... qlArgs)
    Finds one entity of the desired type.
    final void
    Refreshes the persistent state of a given entity from the database, so that a test can verify that persistent state was modified as expected.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • TestDatabase

      public TestDatabase()
  • Method Details

    • refresh

      public final void refresh(BaseEntity entity)
      Refreshes the persistent state of a given entity from the database, so that a test can verify that persistent state was modified as expected.
      Parameters:
      entity - the entity
    • findOne

      public final <E extends BaseEntity> E findOne(String qlStatement, Object... qlArgs)
      Finds one entity of the desired type.
      Type Parameters:
      E - specifies the desired entity type
      Parameters:
      qlStatement - a JPQL statement for finding a list of entities of which the first gets returned
      qlArgs - zero or more arguments for positional parameters in the JPQL statement
      Returns:
      the first entity found, if any, or null if none
    • assertCreated

      public final void assertCreated(BaseEntity newEntity, String qlStatement, Object... qlArgs)
      Verifies that a given entity was properly inserted into the database, including the generation of its integer identifier.
      Parameters:
      newEntity - an entity just persisted
      qlStatement - a JPQL statement for finding a set of entities which must contain the new entity
      qlArgs - zero or more arguments for positional parameters in the JPQL statement
      Throws:
      AssertionError - if the new entity has a null or negative id, or if it is not among the entities found by executing the given JPQL statement