Skip to content

Hibernate generated fields not populated in tests annotated with @DataJpaTest #12679

Closed
@GoGoris

Description

@GoGoris

Using Spring Boot 2.0.0.RELEASE and Junit 5.1.0.

The annotationprocessor for @GeneratorType annotations does not seem to get loaded in tests annotated with @DataJpaTest.

I have a jpa object with a custom Hibernate ValueGenerator that generates a custom project code.
This test fails when it is ran with the @DataJpaTest annotation because the field is not generated at insertion.

My Jpa object has the following field:

@Entity
public class Project {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

    @GeneratorType(type = NummerGenerator.class, when = GenerationTime.INSERT)
    @Column(name = "CODE")
    private String code;

}

This is my testcase, it fails on the last assertion. So the id is generated correctly but the project code is not.
The test passes if I replace @DataJpaTest with @SpringBootTest @AutoConfigureTestDatabase.

@ExtendWith(SpringExtension.class)
@DataJpaTest
public class ProjectRepositoryTest {
        @Autowired
	private ProjectRepository repository;

	@Test
	public void saveTest() {
		Project project = Project.builder().naam("test").build();
		project = repository.save(project);

		assertThat(project).isNotNull();
		assertThat(project.getId()).as("id").isGreaterThan(0L);
		assertThat(project.getCode()).as("Code after insertion").isEqualTo("PREFIX123");
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions