0

I have a scenario where I am consuming an event and saving the details in the DB. Now the record that is being stored in the database has the id field autogenerated @GeneratedValue(strategy = GenerationType.IDENTITY).

In my test case I need to check if data is getting stored in the DB or not and is as per expectation. But I am not sure how will I do findById() of SpringBoot Crud/JPA Repository since I do not know what value got generated.

Any help would be appreciated.

newLearner
  • 637
  • 1
  • 12
  • 20

2 Answers2

0

I think you are looking for annotation @DirtiesContext .

It is a Test annotation which indicates that the ApplicationContext associated with a test is dirty and should therefore be closed and removed from the context cache. - javadoc

Read Section 9.3.4 - Here

Check - Example ans below as well:

@Test
@DirtiesContext
public void save_basic() {
    // get a course
    Course course = courseJpaRepository.findById(10001L);
    assertEquals("JPA ", course.getName());

    // update details
    course.setName("JPA - Updated");
    courseJpaRepository.saveOrUpdate(course);

    // check the value
    Course course1 = courseJpaRepository.findById(10001L);
    assertEquals("JPA - Updated", course1.getName());
}

BTW - how you can get the id : simply via getter method from the return type of save

EmployeeDetails employeeDetails = emaployeeService.saveEmployeeDetails(employee);
int temp = employeeDetails.getID()

Related Post : Here

Tarun
  • 685
  • 3
  • 16
  • Thanks but to be honest, I did not understood how it will solve my issue. If I add the annotation, what should be the value for `id` in `findById()`. Hope I am making sense. – newLearner Jun 08 '21 at 16:04
  • @newLearner You can create the data with some DUMMY ID and then query the DB for this DUMMY id. How it will help - the data you are creating with this DUMMY id will removed from DB once done with test. This way , you can test if the data is getting stored in DB or not. – Tarun Jun 08 '21 at 16:07
  • Ok. So what you're suggesting is that I myself create an entry of the expected data in the database and then query based on that. Right ? – newLearner Jun 08 '21 at 16:10
  • yes ...but you can use method repository as well which can save the data for you . – Tarun Jun 08 '21 at 16:12
  • @newLearner : Updated answer with one example i tried a long ago . which was basically to test the update in DB. – Tarun Jun 08 '21 at 16:15
  • How does this test your changes ? How do you know that actually the DB is saving the details that it consumes from the event when you are testing it against something that you are inserting yourself ? – newLearner Jun 08 '21 at 20:38
  • @newLearner : i am retrieving the record from DB with id 1001 .. and asserting is a proof that the value got from ```findById``` is actually read from DB.... what else you do in unit test - the same asserts !!! – Tarun Jun 08 '21 at 20:53
  • Sorry but it's not unit testing. I am doing an integration test. – newLearner Jun 08 '21 at 21:02
  • @newLearner - updated answer with related post to get id after save. when you say test case - people might refer to unit test ...please specify clearly in question – Tarun Jun 08 '21 at 21:02
0

Take a look at save method from CrudRepository interface. Spring executes this method in transaction and after its completion Hibernate will generate identifier in returned entity.

Suppose your entity and repository looks as following:

....
public class SomeEntity {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    public SomeEntity(String name){
        this.name = name;
    }
....
}

public interface SomeRepository extends CrudRepository<SomeEntity, Long> {
}

After saving entity:

    SomeEntity someEntity = someRepository.save(new SomeEntity("Some entity"));

someEntity.getId() will contain actual record id which can be used further in your tests.

eparvan
  • 1,639
  • 1
  • 15
  • 26