Unit & Integration Testing REST Services

Create JPA Entity

The REST services were based around a resource called AnEntity. The ‘AnEntity’ resources themselves were persisted using JPA.

Select New, Entity Class..., and then use Lombok to tidy up the code and add a simple attribute (called ‘name’) to give:

package com.nigeleke.restin.model;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import lombok.Data;

public @Data class AnEntity implements Serializable {

  public String toString() {
    return "com.nigeleke.restin.AnEntity[ id=" + id + ", name=" + name + "]";

  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  private String name;

  private static final long serialVersionUID = 1L;

This step also create the persistence.xml file using restin as the JNDI name.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="restinPU" transaction-type="JTA">
      <property name="eclipselink.ddl-generation" value="create-tables"/>

Note, at this point, Netbeans enables you to set up a database referenced by the JNDI name. This will not be the database used during integration testing.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.