vendredi 18 mai 2018

Integration testing with spring declarative caching

I'm trying to write integration tests for a Spring Boot 2 Application.

One test should test updating a value via REST.

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureTestEntityManager
@Transactional
public class TenantEndpointIT {
    @Autowired
    private TestRestTemplate template;

    @Autowired
    private TestEntityManager entityManager;

    @Test
    public void nok_updateValueForbidden() {

    }
}

Now, I thought the cleanest way was to create the value with the TestEntityManager in the @Before method, then test the REST endpoint in the actual test.

But the service called by the REST Endpoint is annotated with Spring Caching annotations. So the test fails if I do that. I could use the service directly or make a second REST call. That creates problems with other tests using the same Value, because even if the DB is rolled-back, the cache seems to contain the value. (Now I'm using @DirtiesContext).

My question is, how do you correctly integration test services with @Cachable? Is there a way to get the Cache and explicitly put/remove? I tried autowiring a CacheManager, but it won't find one and fails.

Aucun commentaire:

Enregistrer un commentaire