Annotation Type DataJpaTest


@Documented @Inherited @Retention(RUNTIME) @Target(TYPE) @ResourceLock("com.codename1.backend.test.BackendTest") public @interface DataJpaTest

Tests persistence without the rest of the application: what Spring's @DataJpaTest does.

The slice has the database, the entity manager and the module's repositories, and none of its components, controllers or routes. A test injects the EntityManager, the DataSource or a repository interface and calls it directly. Components it needs too are named in includes(); a dependency of theirs that the slice lacks fails the build.

The database is the one database() names, a fresh in-memory SQLite one by default, whatever the module's settings say, so a persistence test never touches a database that outlives it. The generated daos create their tables in it, and migrations run as they do on start-up.

Each test method runs in a transaction that is rolled back when it returns, as Spring's are, so one test's rows are not the next one's. @BeforeEach and @AfterEach methods run outside it; rollback() set to false commits instead.

  • Element Details

    • includes

      Class<?>[] includes
      Components and configurations of the application to wire into the slice, what Spring's @Import adds to one.
      Default:
      {}
    • database

      String database
      The database URL, as cn1.datasource.url takes it. An in-memory SQLite database by default, which is gone when the test run ends.
      Default:
      ":memory:"
    • rollback

      boolean rollback
      Whether each test method's transaction is rolled back.
      Default:
      true
    • properties

      String[] properties
      Settings for this test, as key=value, as BackendTest.properties().
      Default:
      {}
    • profile

      String profile
      The profile, as BackendTest.profile().
      Default:
      "test"
    • classes

      Class<?>[] classes
      Default:
      {}