I'm trying to write an integration test for a java spring project.
At some point in the code I need to check customer.getId(), where the ID is created by a @Formula annotation like this: (keep in mind Customer_Id is my auto-incrementing primary key)
@Formula("Customer_Id")
private int id
I use lombok to generate getters and setters methods. My database for testing is H2 and I use Junit5 for my tests. The problem i'm facing is that i cannot test the getId() behavior because it's always 0 when I annotate the tests with @DataJpaTest. I made it work with @SpringBootTest but I would like to stick to sliced tests.
Is there a way to make @DataJpaTest work with @Formulas ?
Thanks in advance.
@Entity(name="customers")
@Table(name="Customers")
@Getter
@Setter
@NoArgsConstructor
public class Customer{
@Formula("Customer_Id")
private int id;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="Customer_Id", unique=true, nullable=false, precision=10)
private int customerId;
}
// if it is annotated with @SpringBootTest the tests passes
@DataJpaTest
@ActiveProfiles("test")
@EnableJpaAuditing
class MyClassTests {
@Autowired
private CustomerRepository customerRepository;
@Test
void given_a_customer_should_have_customerId_and_id_equal(){
Customer customer = new Customer();
customerRepository.save(customer);
// if i don't call findOneByCustomerId test fails for both @SpringBootTest and @DataJpaTest
customer = customerRepository.findOneByCustomerId(customer.getCustomerId());
assertEquals(customer.getCustomerId(), customer.getId()); // expected: 1, actual 0
}
}