Related: Trouble Shooting

First Entity

Create a package org.example.domain and in that create an entity bean like Customer.kt

package org.example.domain

import javax.persistence.Entity
import javax.persistence.Id

@Entity
class Customer {

  @Id
  var id: Long = 0

  var name: String? = null

}

Add ebean.mf

In src/main/resources add ebean.mf which tells the build plugins which packages should be enhanced.

entity-packages: org.example.domain
transactional-packages: org.example
querybean-packages: org.example

If you have the ebeaninit command line tool installed you can run it and get it to generate the ebean.mf manifest file.

If your entity beans are in a different package (not org.example.domain) then adjust the ebean.mf file to suit.

Add application-test.yml

In src/test/resources add application-test.yml which tells Ebean how to run tests.

ebean:
  test:
#    shutdown: stop # stop | remove
    platform: h2 # h2, postgres, mysql, oracle, sqlserver, sqlite
    ddlMode: dropCreate # none | dropCreate | create | migration | createOnly | migrationDropCreate
    dbName: myapp

This tells Ebean which DDL mode to use like dropCreate or migration. dropCreate is mode we would use the most (and we haven't generated migrations yet).

If we chose a database platform like Postgres, MySql, SQL Server or Oracle then a docker container will automatically be started and setup to run the tests against.

ebeaninit can add a application-test.ymll if you have it installed.

Add logback-test.yml (optional)

We don't have to setup logging but it is a good idea to be able to see the DDL and SQL that is being generated and executed to help understand what is going on.

Example logback entries (add to logback-test.xml)
<!-- Testing with Docker containers -->
<logger name="io.ebean.docker" level="TRACE"/>

<logger name="io.ebean.DDL" level="TRACE"/>

<logger name="io.ebean.SQL" level="TRACE"/>
<logger name="io.ebean.TXN" level="TRACE"/>
<logger name="io.ebean.SUM" level="TRACE"/>

<!-- L2 cache logging -->
<logger name="io.ebean.cache.QUERY" level="TRACE"/>
<logger name="io.ebean.cache.BEAN" level="TRACE"/>
<logger name="io.ebean.cache.COLL" level="TRACE"/>
<logger name="io.ebean.cache.NATKEY" level="TRACE"/>

ebeaninit can add a logback-test.xml if you have it installed.

First Test

Create a test in src/test like CustomerTest.java

package org.example.domain

import io.ebean.Ebean
import org.junit.Test

class CustomerTest  {

  @Test
  fun insert_update_delete() {

    val customer = Customer()
    customer.name  = "Hello entity bean"

    // insert
    Ebean.save(customer)

    // update
    customer.name = "Goodbye"
    Ebean.save(customer)

    // delete the bean
    Ebean.delete(customer);
  }
}

Run test

Run the test via the IDE and via Maven or Gradle. Check the logs to confirm you see the DDL and SQL that you expect.

23:40:09.040 [main] INFO  io.ebean.EbeanVersion - ebean version: 11.26.1
23:40:09.087 [main] INFO  i.e.config.properties.LoadContext - loaded properties from [application.yml, application-test.yml]
23:40:09.163 [main] INFO  i.e.t.c.provider.ProviderAutoConfig - for testing purposes a current user and tenant provider has been configured. Use io.ebean.test.UserContext to set current user and tenant in tests.
23:40:09.190 [main] INFO  o.a.datasource.pool.ConnectionPool - DataSourcePool [db] autoCommit[false] transIsolation[READ_COMMITTED] min[2] max[200]
23:40:09.297 [main] INFO  io.ebean.internal.DefaultContainer - DatabasePlatform name:db platform:h2
23:40:09.550 [main] INFO  io.ebean.DDL - Executing db-drop-all.sql - 1 statements
23:40:09.551 [main] DEBUG io.ebean.DDL - executing 1 of 1 drop table if exists customer
23:40:09.557 [main] INFO  io.ebean.DDL - Executing db-create-all.sql - 1 statements
23:40:09.557 [main] DEBUG io.ebean.DDL - executing 1 of 1 create table customer ( id                            bigint auto_increment not...
23:40:09.587 [main] DEBUG io.ebean.SQL - txn[1001] insert into customer (name) values (?); --bind(Hello entity bean)
23:40:09.608 [main] DEBUG io.ebean.SUM - txn[1001] Inserted [Customer] [1]
23:40:09.609 [main] DEBUG io.ebean.TXN - txn[1001] Commit
23:40:09.615 [main] DEBUG io.ebean.SQL - txn[1002] update customer set name=? where id=?; --bind(Goodbye,1)
23:40:09.616 [main] DEBUG io.ebean.SUM - txn[1002] Updated [Customer] [1]
23:40:09.617 [main] DEBUG io.ebean.TXN - txn[1002] Commit
23:40:09.618 [main] DEBUG io.ebean.SQL - txn[1003] delete from customer where id=?; --bind(1)
23:40:09.619 [main] DEBUG io.ebean.SUM - txn[1003] Deleted [Customer] [1]
23:40:09.619 [main] DEBUG io.ebean.TXN - txn[1003] Commit

Edit Page