Related: Trouble Shooting

First Entity

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

package org.example.domain;

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

@Entity
public class Customer {

  @Id
  long id;

  String name;

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

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 org.junit.Test;

import io.ebean.Ebean;
import io.ebean.EbeanServer;

public class CustomerTest {

 @Test
 public void insertFindDelete() {

  Customer customer = new Customer();
  customer.setName("Hello world");


  EbeanServer server = Ebean.getDefaultServer();

  // insert the customer in the DB
  server.save(customer);


  // Find by Id
  Customer foundHello = server.find(Customer.class, 1);

  System.out.print("hello " + foundHello.getName());

  // delete the customer
  server.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.

00:49:58.787 [main] INFO  io.ebean.EbeanVersion - ebean version: 11.26.1
00:49:58.851 [main] INFO  i.e.config.properties.LoadContext - loaded properties from [application.yml, application-test.yml]
00:49:58.921 [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.
00:49:58.943 [main] INFO  o.a.datasource.pool.ConnectionPool - DataSourcePool [db] autoCommit[false] transIsolation[READ_COMMITTED] min[2] max[200]
00:49:59.063 [main] INFO  io.ebean.internal.DefaultContainer - DatabasePlatform name:db platform:h2
00:49:59.242 [main] INFO  io.ebean.DDL - Executing db-drop-all.sql - 1 statements
00:49:59.242 [main] DEBUG io.ebean.DDL - executing 1 of 1 drop table if exists customer
00:49:59.248 [main] INFO  io.ebean.DDL - Executing db-create-all.sql - 1 statements
00:49:59.248 [main] DEBUG io.ebean.DDL - executing 1 of 1 create table customer ( id                            bigint auto_increment not...
00:49:59.271 [main] DEBUG io.ebean.SQL - txn[1001] insert into customer (name) values (?); --bind(Hello world)
00:49:59.290 [main] DEBUG io.ebean.SUM - txn[1001] Inserted [Customer] [1]
00:49:59.291 [main] DEBUG io.ebean.TXN - txn[1001] Commit
00:49:59.318 [main] DEBUG io.ebean.SQL - txn[1003] select t0.id, t0.name from customer t0 where t0.id = ?  ; --bind(1, )
00:49:59.320 [main] DEBUG io.ebean.SUM - txn[1003] FindBean type[Customer] origin[D88YBi.A.A] exeMicros[3324] rows[1] bind[1, ]
00:49:59.320 [main] DEBUG io.ebean.SQL - txn[1004] select t0.id, t0.name from customer t0 where t0.id = ?  ; --bind(1, )
00:49:59.321 [main] DEBUG io.ebean.SUM - txn[1004] FindBean type[Customer] origin[D88YBi.A.A] exeMicros[247] rows[1] bind[1, ]
hello Hello world 00:49:59.322 [main] DEBUG io.ebean.SQL - txn[1005] delete from customer where id=?; --bind(1)
00:49:59.323 [main] DEBUG io.ebean.SUM - txn[1005] Deleted [Customer] [1]
00:49:59.323 [main] DEBUG io.ebean.TXN - txn[1005] Commit

Edit Page