Home > Blockchain >  @Convert not considered in maven integration test
@Convert not considered in maven integration test

Time:11-29

I have a simple LocalDateTime - Timestamp - Converter used in an @Entity like following:

@Converter(autoApply = true)
public class TimestampConverter implements AttributeConverter<LocalDateTime, Timestamp> {

  @Override
  public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
    return localDateTime == null ? null : DateUtil.convertToTimestamp(localDateTime);
  }

  @Override
  public LocalDateTime convertToEntityAttribute(Timestamp timestamp) {
    return timestamp == null ? null : DateUtil.convertToLocalDateTime(timestamp);
  }
}

This is used in an entity on LocalDateTime field like following:

@Entity
@Table(name = "LOG")
@Data
public class Log implements Serializable {

  @Id
  private long id;

  private String content;

  @Column(name = "DATE_RECORDED")
  @Convert(converter = TimestampConverter.class)
  private LocalDateTime dateRecorded;
}

This conversion is needed to avoid following error on empty (null) values:

ORA-00932: inconsistent datatypes: expected TIMESTAMP got BINARY

Assume having following test executed locally against oracle-xe testcontainer works perfectly fine:

@Testcontainers
public class DbLogTest {

  protected static EntityManager entityManager;

  @Container
  protected static OracleContainer oracle = new OracleContainer();
  
  private static Map<String, String> getProperties() {
    Map<String, String> properties = new HashMap<>();
    properties.put("javax.persistence.jdbc.url", oracle.getJdbcUrl());
    properties.put("javax.persistence.jdbc.user", oracle.getUsername());
    properties.put("javax.persistence.jdbc.password", oracle.getPassword());
    return properties;
  }
  
  @BeforeAll
  static void beforeAll() {
    entityManager = Persistence.createEntityManagerFactory("persistenceUnitName", getProperties()).createEntityManager();
  }
  
  @Test
  public void insertLog() throws Exception {
    Log log = new Log();
    entityManager.persist(new Log());
    entityManager.flush();
    assertTrue(log.getId() > 0);
  }
}

Execution of same test with maven build (mvn test) fails due to the error mentioned above. Debugging shows that converter is not invoked at all when executed in maven test.

Can anyone point me into the right direction understanding and finally solving this issue? What is the difference when executing a test inside IDE compared to maven?

As workaround I could abstain from using LocalDateTime but anyway would I like to understand that issue.

CodePudding user response:

The issue was caused by MultiModuleBuild. Entity and Converter are in a different module than the DbLogTest. As the entire build has not been conducted the maven test run of DbLogTest resulted in the observed behavior. Performing clean install solved the problem.

  • Related