1111import org .springframework .boot .SpringApplication ;
1212import org .springframework .context .ConfigurableApplicationContext ;
1313import org .testcontainers .containers .GenericContainer ;
14+ import io .lettuce .core .RedisClient ;
15+ import io .lettuce .core .api .StatefulRedisConnection ;
1416
1517import java .util .List ;
1618import java .util .Map ;
@@ -33,6 +35,8 @@ public class EmbeddedEvoMasterController extends EmbeddedSutController {
3335
3436 private static final int RATING_STARS = 5 ;
3537
38+ private static final String CACHE_KEY_PATTERN = "id.my.hendisantika.springbootredissample.*Cache::*" ;
39+
3640 private static final GenericContainer redis = new GenericContainer ("redis:" + REDIS_VERSION )
3741 .withCommand ("redis-server" , "--requirepass" , REDIS_PASSWORD )
3842 .withExposedPorts (REDIS_PORT );
@@ -54,6 +58,10 @@ public static void main(String[] args) {
5458
5559 private ConfigurableApplicationContext ctx ;
5660
61+ private RedisClient redisClient ;
62+
63+ private StatefulRedisConnection <String , String > redisConnection ;
64+
5765 public EmbeddedEvoMasterController () {
5866 this (0 );
5967 }
@@ -67,6 +75,9 @@ public EmbeddedEvoMasterController(int port) {
6775 public String startSut () {
6876
6977 redis .start ();
78+ redisClient = RedisClient .create ("redis://:" + REDIS_PASSWORD + "@"
79+ + redis .getHost () + ":" + redis .getMappedPort (REDIS_PORT ));
80+ redisConnection = redisClient .connect ();
7081
7182 ctx = SpringApplication .run (SpringBootRedisSampleApplication .class , new String []{
7283 "--server.port=0" ,
@@ -102,6 +113,13 @@ public void stopSut() {
102113 ctx = null ;
103114 }
104115
116+ if (redisConnection != null ) {
117+ redisConnection .close ();
118+ redisClient .shutdown ();
119+ redisConnection = null ;
120+ redisClient = null ;
121+ }
122+
105123 redis .stop ();
106124 }
107125
@@ -110,8 +128,19 @@ public String getPackagePrefixesToCover() {
110128 return "id.my.hendisantika.springbootredissample." ;
111129 }
112130
131+ /*
132+ Only the Spring Cache entries accumulate: the API is read-only, and the seeded data is
133+ written once at startup by runners guarded on an empty repository, so it must survive.
134+ */
113135 @ Override
114136 public void resetStateOfSUT () {
137+ if (redisConnection == null ) {
138+ return ;
139+ }
140+ List <String > keys = redisConnection .sync ().keys (CACHE_KEY_PATTERN );
141+ if (!keys .isEmpty ()) {
142+ redisConnection .sync ().del (keys .toArray (new String [0 ]));
143+ }
115144 }
116145
117146
0 commit comments