I am using Redis embedded server for my integrations tests and I noticed that using the RedisServer.stop()
method is not really stoping the mock server. Any idea how to force kill it in my test cases?
Dependency:
<!-- Redis test mock server --><dependency><groupId>it.ozimov</groupId><artifactId>embedded-redis</artifactId><version>0.7.2</version><scope>test</scope></dependency>
Cache integration test:
@SpringBootTest@ActiveProfiles("test")@ExtendWith({SpringExtension.class, RestDocumentationExtension.class})class CacheServiceImplTest { private RedisServer redisServer; @MockBean private StoreRepository repo; @Autowired public ObjectMapper objectMapper; @Autowired private CacheService cacheService; @PostConstruct public void postConstruct() {// if (redisServer.isActive()) {//// redisServer.stop();// Runtime.getRuntime().addShutdownHook(new Thread() {// public void run() { redisServer.stop(); }// });// } redisServer.start(); } @PreDestroy public void preDestroy() {// redisServer.stop(); Runtime.getRuntime().addShutdownHook(new Thread(() -> redisServer.stop())); redisServer.stop(); } @Test void saveStoreToRedis() { List<Store> stores = new ArrayList<>(); initializeStores(stores); when(repo.findAll()).thenReturn(stores); List<StoreResponse> actualStoresResponse = storeDao.findAll(); Assert.assertNotNull(actualStoresResponse);// cacheService.invalidateCache(); }}
I am getting error
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxx.CacheServiceImplTest.ORIGINAL': Invocation of init method failed; nested exception is java.lang.RuntimeException: Can't start redis server. Check logs for details.
But when I kill it manually things work just fine.
lsof -i:6370COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEredis-ser 80714 XXX 4u IPv4 0x5c18b66bd501f58d 0t0 TCP localhost:6370 (LISTEN) kill -9 80714