1

I use Testcontainers to run integration tests in a MySQL container. I have an entity Listing which has a OneToMany mapping to a child entity, Variations. I have a method in my service layer which updates the variations for an associated listing (note it removes all existing variations beforehand):

public ListingDTO updateListingVariations(String listingId, List<ListingVariationDTO> variations) {
    listingVariationRepository.deleteAllByListingId(listingId);

    return listingRepository.findByListingId(listingId)
            .map(listing -> {
                List<ListingVariation> newVariations = variations.stream()
                        .map(this::toListingVariation)
                        .toList();
                listing.setVariations(newVariations);
                return this.toListingDTO(listingRepository.save(listing));
            })
            .orElseThrow(() -> new ListingNotFoundException(listingId));
}

Now I perform a test on this method (assume the database already contains a Listing with some associated ListingVariation's):

@Test
void shouldUpdateListingVariationsIfListingIdExists() {
    // builders for ListingVariation objects

    assertThat(listingService.findByListingId(validListingId).getVariations())
            .isEqualTo(listingVariations);
    assertThat(listingService.updateListingVariations(validListingId, newVariations).getVariations())
            .isEqualTo(newVariations);
}

However, the test is failing with the below error:

expected: [ListingVariationDTO{id=1, listingId='listingId', ...}]
 but was: [ListingVariationDTO{id=4, listingId='listingId', ...}]

I realise this is due to the auto-increment on the table not resetting when the previous variation records are deleted. I could modify my test to build a new ListingVariation object and hardcode the ID as 4, but I would like to avoid doing that for the simplicity of my test code. Can I reset the auto-increment on my table through my MySQL container?

juyebgastro
  • 51
  • 1
  • 2
  • 5

1 Answers1

1

You can do this by executing a SQL statement.

ALTER TABLE tablename AUTO_INCREMENT = 1

Which you probably would do using a JdbcTemplate.

But I'd argue you shouldn't check the value of the id at all in your test. Just check that the id is not null. The reason this is the correct approach is that (hopefully) nobody cares about the id. It is just an id and it doesn't matter if it is 1, 2, 4, or 314159265, so your test shouldn't care either.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • That makes sense, I agree omitting the ID would be ideal as we do not care about that in the test. However, if I don't supply the ID to the builder method which constructs my `ListingVariation`, then the ID will be set to null. So how would I even go about omitting the ID? – juyebgastro Nov 10 '22 at 10:05
  • You can set the id to whatever you get from whatever you want to compare it to. Or you ignore it in the comparison by building a custom comparision. This sounds like the most promising approach to me. See https://www.baeldung.com/assertj-custom-assertion – Jens Schauder Nov 10 '22 at 11:04