Skip to content

java

3 posts with the tag “java”

How to resolve module java.base does not 'opens java.lang' to unnamed module?

1. Purpose

In this post, I will demonstrate how to resolve the following error when running spring cloud eureka server:

Terminal window
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run (default-cli) on project service-discovery: An exception occurred while running. null: InvocationTargetException: Cannot load configuration class: org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration: ExceptionInInitializerError: Unable to load cache item: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @505e2a79 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

The key problem is below:

Terminal window
java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module ...

2. The solution

The detailed error

I want to start an spring cloud eureka server in IntelliJ IDEA, but I got this error:

Terminal window
/Library/Java/JavaVirtualMachines/temurin-16.jdk/Contents/Home/bin/java "-Dmaven.multiModuleProjectDirectory=/Users/bswen/JavaProjects/SpringCloudLearning202201/springcloud-demo/chapter03 -- hello-cloud/service-discovery" "-Dmaven.home=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3" "-Dclassworlds.conf=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/bin/m2.conf" "-Dmaven.ext.class.path=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven-event-listener.jar" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds-2.6.0.jar" org.codehaus.classworlds.Launcher -Didea.version2019.3.3 org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run
[INFO] Scanning for projects...
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @505e2a79
at java.lang.reflect.AccessibleObject.checkCanSetAccessible (AccessibleObject.java:357)
...
at com.cd826dong.clouddemo.Application.main (Application.java:32)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:78)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:567)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:527)
at java.lang.Thread.run (Thread.java:831)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run (default-cli) on project service-discovery: An exception occurred while running. null: InvocationTargetException: Cannot load configuration class: org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration: ExceptionInInitializerError: Unable to load cache item: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @505e2a79 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

The module’s dependency

The source code

The Eureka Server’s source code:

@EnableEurekaServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}

The parent module’s dependency:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<groupId>cd826dong.cloud</groupId>
<artifactId>springcloud-book-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>SpringCloud Demo Projects(HelloCloud) -- Parent Pom</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<guava.version>20.0</guava.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

This module’s dependency:

<parent>
<groupId>cd826dong.cloud</groupId>
<artifactId>springcloud-book-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>
<artifactId>service-discovery</artifactId>
<packaging>jar</packaging>
<name>SpringCloud Demo Projects(HelloCloud) -- Eureka Server</name>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

if start from command line, we got this error:

Terminal window
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run (default-cli) on project springcloud-book-parent: Unable to find a suitable main class, please add a 'mainClass' property -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Let’s Debug the error

Check the java version:

Terminal window
chapter03 -- hello-cloud git:(master) java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

The solution

Run the following command in parent module:

Terminal window
mvn install

Then re-run the module:

mvn spring-boot:run

But double-clicking the run to run the spring-boot plugin in the idea still reports an error:

The reason of the error

Idea use jdk 1.6 to start spring applicaiton, just as the logs show:

/Library/Java/JavaVirtualMachines/temurin-16.jdk/Contents/Home/bin/java ...

Then I check:

➜ chapter03 -- hello-cloud git:(master) ✗ /Library/Java/JavaVirtualMachines/temurin-16.jdk/Contents/Home/bin/java --version
openjdk 16.0.2 2021-07-20
OpenJDK Runtime Environment Temurin-16.0.2+7 (build 16.0.2+7)
OpenJDK 64-Bit Server VM Temurin-16.0.2+7 (build 16.0.2+7, mixed mode, sharing)
➜ chapter03 -- hello-cloud git:(master) ✗

It’s jdk1.6, NOT jdk1.8, that is the problem.

Change project sdk to 1.8:

Then click ‘spring-boot:run’ to run again:

2022-01-13 17:27:30.757 INFO 10418 --- [ Thread-12] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2022-01-13 17:27:30.766 INFO 10418 --- [ Thread-12] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2022-01-13 17:27:30.828 INFO 10418 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8260 (http)
2022-01-13 17:27:30.829 INFO 10418 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8260
2022-01-13 17:27:30.835 INFO 10418 --- [ main] com.cd826dong.clouddemo.Application : Started Application in 9.384 seconds (JVM running for 13.583)

It works!

How to change default JAVA sdk version for new java projects?

Click File--Other Settings--Structure for new projects:

then change default sdk for intellij idea:

3. Summary

In this post, I demonstrated how to solve the module java.base does not "opens java.lang" to unnamed module error. The key point is to make sure that your java version is correct. That’s it, thanks for your reading.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

How to connect to multiple redis server when using spring boot ?

Purpose

In this article , I will demo how to connect to multiple redis servers using spring boot. Just as following picture shows, if you don’t know how to setup two redis servers in your environment, you can refer to this post.

image-20210305165358100

Environment

  • Spring boot 2+
  • Jdk 8+
  • Redis

Prepare the test data in redis

I deployed two redis docker container bw-redis1 and bw-redis2, now I use command line to set some values in them:

(base) [root@local ~]# docker exec -it bw-redis1 redis-cli
127.0.0.1:6379> set k1 redis1
OK
127.0.0.1:6379> get k1
"redis1"
127.0.0.1:6379> exit
(base) [root@local ~]# docker exec -it bw-redis2 redis-cli
127.0.0.1:6379> set k1 redis2
OK
127.0.0.1:6379> get k1
"redis2"
127.0.0.1:6379> exit
(base) [root@local ~]#

The Project Layout

The layout of this demo project is as follows, all the example code can be found here:

spring-boot-multiredis git:(master) tree .
.
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │   └── bswen
│   │   │   └── sbmr
│   │   │   ├── MultiRedisApplication.java
│   │   │   ├── config
│   │   │   │   ├── Redis1Configuration.java
│   │   │   │   ├── Redis1Property.java
│   │   │   │   ├── Redis2Configuration.java
│   │   │   │   ├── Redis2Property.java
│   │   │   │   └── RedisCommonProperty.java
│   │   │   └── service
│   │   │   ├── MultiRedisTestRunner.java
│   │   └── resources
│   │   └── application.properties

The Pom

We use maven to define the dependencies, I excluded the default lettuce sdk, because it’s not stable. I’d rather use the jedis sdk instead.

pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<artifactId>spring-boot-multiredis</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>

The code

1. Config file

Here in the src/main/resources/application.properties, I defined two redis server configurations,

src/main/resources/application.properties
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=0
spring.redis2.host=localhost
spring.redis2.port=26379
spring.redis2.database=0

Here we define two redis connection, one is localhost:6379, the other is localhost:26379. You should change them to your real configurations.

2. Property config classes

To read the above properties file into spring boot, we must define configuration classes:

  1. At first, we define a common class as the base class of redis configuration classes
src/main/java/com.bswen.sbmr/config/RedisCommonProperty.java
public class RedisCommonProperty {
private String host;
private int port;
private int database;
//getters setters
}
  1. Then, we define Redis1Property and Redis2Property class to denote the different redis configurations
src/main/java/com.bswen.sbmr/config/Redis1Property.java
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
public class Redis1Property extends RedisCommonProperty {
}

And this:

src/main/java/com.bswen.sbmr/config/Redis2Property.java
@Configuration
@ConfigurationProperties(prefix = "spring.redis2")
public class Redis2Property extends RedisCommonProperty {
}

Pay attention to the ConfigurationProperties annotation, we use it to load different properties from the application.properties file.

  1. Thirdly, we should define how to get the RedisTemplate and RedisConnectionFactory from above properties

For redis server1, we define Redis1Configuration:

src/main/java/com.bswen.sbmr/config/Redis1Configuration.java
@Configuration
public class Redis1Configuration {
@Autowired
private Redis1Property redis1Property;
@Primary
@Bean(name = "redis1ConnectionFactory")
public RedisConnectionFactory userRedisConnectionFactory() {
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName(redis1Property.getHost());
redisConnectionFactory.setPort(redis1Property.getPort());
redisConnectionFactory.setDatabase(redis1Property.getDatabase());
return redisConnectionFactory;
}
@Bean(name = "redis1StringRedisTemplate")
public StringRedisTemplate userStringRedisTemplate(@Qualifier("redis1ConnectionFactory") RedisConnectionFactory cf) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(cf);
return stringRedisTemplate;
}
@Bean(name = "redis1RedisTemplate")
public RedisTemplate userRedisTemplate(@Qualifier("redis1ConnectionFactory") RedisConnectionFactory cf) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(cf);
return stringRedisTemplate;
}
}

To avoid the following error:

***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean named 'redisTemplate' that could not be found.

We need to change Redis1Configuration to this:

src/main/java/com.bswen.sbmr/config/Redis1Configuration.java
@Configuration
public class Redis1Configuration {
@Autowired
private Redis1Property redis1Property;
@Primary
@Bean(name = "redis1ConnectionFactory")
public RedisConnectionFactory userRedisConnectionFactory() {
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName(redis1Property.getHost());
redisConnectionFactory.setPort(redis1Property.getPort());
redisConnectionFactory.setDatabase(redis1Property.getDatabase());
return redisConnectionFactory;
}
@Bean(name = "redis1StringRedisTemplate")
public StringRedisTemplate userStringRedisTemplate(@Qualifier("redis1ConnectionFactory") RedisConnectionFactory cf) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(cf);
return stringRedisTemplate;
}
@Bean(name = "redis1RedisTemplate")
@Bean(name = {"redis1RedisTemplate","redisTemplate"})
public RedisTemplate userRedisTemplate(@Qualifier("redis1ConnectionFactory") RedisConnectionFactory cf) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(cf);
return stringRedisTemplate;
}
}

Because springboot2 needs a default bean named redisTemplate, so we reuse the redis1RedisTemplate bean, add a name redisTemplate to it.

For redis server2, we define Redis2Configuration:

src/main/java/com.bswen.sbmr/config/Redis2Configuration.java
@Configuration
public class Redis2Configuration {
@Autowired
private Redis2Property redis2Property;
@Bean(name = "redis2ConnectionFactory")
public RedisConnectionFactory roleRedisConnectionFactory() {
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName(redis2Property.getHost());
redisConnectionFactory.setPort(redis2Property.getPort());
redisConnectionFactory.setDatabase(redis2Property.getDatabase());
return redisConnectionFactory;
}
@Bean(name = "redis2StringRedisTemplate")
public StringRedisTemplate roleStringRedisTemplate(@Qualifier("redis2ConnectionFactory") RedisConnectionFactory cf) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(cf);
return stringRedisTemplate;
}
@Bean(name = "redis2RedisTemplate")
public RedisTemplate roleRedisTemplate(@Qualifier("redis2ConnectionFactory") RedisConnectionFactory cf) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(cf);
//setSerializer(stringRedisTemplate);
return stringRedisTemplate;
}
}

Now we have setup the properties and the configurations , we have beans of RedisTemplate , we can test it as follows:

src/main/java/com.bswen.sbmr/service/MultiRedisTestRunner.java
@Component
public class MultiRedisTestRunner implements CommandLineRunner {
private final static Logger logger = LoggerFactory.getLogger(MultiRedisApplication.class);
@Autowired
@Qualifier("redis1StringRedisTemplate")
private StringRedisTemplate stringRedisTemplate;
@Autowired
@Qualifier("redis2StringRedisTemplate")
private StringRedisTemplate stringRedis2Template;
//@Override
public void run(String... strings) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
try {
testRedis1();
testRedis2();
}finally {
latch.await();
}
}
private void testRedis2() {
try {
for (int i = 1; i < 2; i++) {
logger.info("=====================================================================");
logger.info("start loop redis 2:" + i);
String key = "k" + i;
String primaryKeyValue = stringRedis2Template.opsForValue().get(key);
logger.info("=====================================================================");
logger.info(String.format("read from the redis2, key %s value is %s", key, primaryKeyValue));
}
}finally {
}
}
private void testRedis1() {
try {
for (int i = 1; i < 2; i++) {
logger.info("=====================================================================");
logger.info("start loop redis 1:" + i);
String key = "k" + i;
String primaryKeyValue = stringRedisTemplate.opsForValue().get(key);
logger.info("=====================================================================");
logger.info(String.format("read from the redis1, key %s value is %s", key, primaryKeyValue));
}
}finally {
}
}
}

Run it, we get this result:

Terminal window
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)
2024-12-30 18:05:56.099 WARN 49528 --- [ main] o.s.boot.StartupInfoLogger : InetAddress.getLocalHost().getHostName() took 5002 milliseconds to respond. Please verify your network configuration (macOS machines may need to add entries to /etc/hosts).
2024-12-30 18:06:01.116 INFO 49528 --- [ main] com.bswen.sbr2.MultiRedisApplication2 : Starting MultiRedisApplication2 using Java 1.8.0_341 on MacBook-Pro-zzz.local with PID 49528 (/Users/zzz/....../bswen-project/spring-boot-multiredis2/target/classes started by zzz in /Users/zzz/private/bw/bswen-github/bswen-project)
2024-12-30 18:06:01.117 INFO 49528 --- [ main] com.bswen.sbr2.MultiRedisApplication2 : No active profile set, falling back to default profiles: default
2024-12-30 18:06:01.509 INFO 49528 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2024-12-30 18:06:01.512 INFO 49528 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2024-12-30 18:06:01.533 INFO 49528 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 6 ms. Found 0 Redis repository interfaces.
2024-12-30 18:06:02.058 INFO 49528 --- [ main] com.bswen.sbr2.MultiRedisApplication2 : Started MultiRedisApplication2 in 21.403 seconds (JVM running for 21.935)
2024-12-30 18:06:02.060 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : =====================================================================
2024-12-30 18:06:02.060 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : start loop redis 1:1
2024-12-30 18:06:02.187 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : =====================================================================
2024-12-30 18:06:02.188 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : read from the redis1, key k1 value is redis1
2024-12-30 18:06:02.188 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : =====================================================================
2024-12-30 18:06:02.188 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : start loop redis 2:1
2024-12-30 18:06:03.286 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : =====================================================================
2024-12-30 18:06:03.287 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : read from the redis2, key k1 value is redis2

It works!

Summary

In this post, I demonstrated how to connect to multiple redis server instances with spring boot 2, the key point is that you should setup specific configuration for each redis server connection. Thanks for your reading. All code of the project is uploaded to github.com, you can download at this link.

Final Words + More Resources

My intention with this article was to help others who might be considering using multiple redis in one spring boot application. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: [email protected].

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

⭐️ Thanks For Your Support 🙏

How to solve IllegalArgumentException: Host must not be empty when using springboot data redis and lettuce?

1. Purpose

In this post, I will demo how to solve the below exception or error when using spring boot data redis and lettuce:

Terminal window
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)
2021-03-21 15:27:30.705 INFO 61218 --- [ Test worker] c.bswen.app10.service.TestRedisService : Starting TestRedisService using Java 1.8.0_121 on MBP-bswen with PID 61218 (started by bswen in /Users/bswen/private/bw/bswen-github/bswen-springboot23/app10)
2021-03-21 15:27:30.706 INFO 61218 --- [ Test worker] c.bswen.app10.service.TestRedisService : The following profiles are active: test
2021-03-21 15:27:31.572 INFO 61218 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-03-21 15:27:31.576 INFO 61218 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2021-03-21 15:27:31.599 INFO 61218 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 7 ms. Found 0 Redis repository interfaces.
2021-03-21 15:27:32.011 WARN 61218 --- [ Test worker] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisService': Unsatisfied dependency expressed through field 'redisClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisClient' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
2021-03-21 15:27:32.035 INFO 61218 --- [ Test worker] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-03-21 15:27:32.088 ERROR 61218 --- [ Test worker] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisService': Unsatisfied dependency expressed through field 'redisClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisClient' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
...
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_121]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisClient' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
.... org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56) ~[na:na]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_121]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisService': Unsatisfied dependency expressed through field 'redisClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisClient' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.3.3.jar:5.3.3]
org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.3.jar:5.3.3]
... 122 common frames omitted
Caused by: java.lang.IllegalArgumentException: Host must not be empty
at io.lettuce.core.internal.LettuceAssert.notEmpty(LettuceAssert.java:44) ~[lettuce-core-6.0.2.RELEASE.jar:6.0.2.RELEASE]
at io.lettuce.core.RedisURI$Builder.redis(RedisURI.java:1151) ~[lettuce-core-6.0.2.RELEASE.jar:6.0.2.RELEASE]
at com.bswen.app10.config.RedisConfiguration.redisClient(RedisConfiguration.java:17) ~[main/:na]
at com.bswen.app10.config.RedisConfiguration$$EnhancerBySpringCGLIB$$fd54fa1a.CGLIB$redisClient$0(<generated>) ~[main/:na]
at com.bswen.app10.config.RedisConfiguration$$EnhancerBySpringCGLIB$$fd54fa1a$$FastClassBySpringCGLIB$$5b3d5194.invoke(<generated>) ~[main/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.3.jar:5.3.3]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.3.jar:5.3.3]
at com.bswen.app10.config.RedisConfiguration$$EnhancerBySpringCGLIB$$fd54fa1a.redisClient(<generated>) ~[main/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.3.jar:5.3.3]
... 123 common frames omitted
Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:355)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$7(ClassBasedTestDescriptor.java:350)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312)
at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:743)
at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:742)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:349)
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
... org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
... 90 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisClient' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
... 122 more
Caused by: java.lang.IllegalArgumentException: Host must not be empty
at io.lettuce.core.internal.LettuceAssert.notEmpty(LettuceAssert.java:44)
at io.lettuce.core.RedisURI$Builder.redis(RedisURI.java:1151)
at com.bswen.app10.config.RedisConfiguration.redisClient(RedisConfiguration.java:17)
at com.bswen.app10.config.RedisConfiguration$$EnhancerBySpringCGLIB$$fd54fa1a.CGLIB$redisClient$0(<generated>)
at com.bswen.app10.config.RedisConfiguration$$EnhancerBySpringCGLIB$$fd54fa1a$$FastClassBySpringCGLIB$$5b3d5194.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
at com.bswen.app10.config.RedisConfiguration$$EnhancerBySpringCGLIB$$fd54fa1a.redisClient(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 123 more
TestRedisService > testGetAndSet() FAILED
java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException at AutowiredAnnotationBeanPostProcessor.java:643
Caused by: org.springframework.beans.factory.BeanCreationException at ConstructorResolver.java:658
Caused by: org.springframework.beans.BeanInstantiationException at SimpleInstantiationStrategy.java:185
Caused by: java.lang.IllegalArgumentException at LettuceAssert.java:44
1 test completed, 1 failed
> Task :app10:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app10:test'.
> There were failing tests. See the results at: file:///Users/bswen/private/bw/bswen-github/bswen-springboot23/app10/build/test-results/test/
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 8s
6 actionable tasks: 5 executed, 1 up-to-date

The core exception is :

Terminal window
[com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
... 122 more
Caused by: java.lang.IllegalArgumentException: Host must not be empty
at io.lettuce.core.internal.LettuceAssert.notEmpty(LettuceAssert.java:44)
at io.lettuce.core.RedisURI$Builder.redis(RedisURI.java:1151)
at com.bswen.app10.config.RedisConfiguration.redisClient(RedisConfiguration.java:17)
at

2. The Environment

  • Jdk 8+
  • Spring boot 2+
  • Gradle 6+

3. The code and solution

3.1 The project structure

The project’s main package is com.bswen.app10, its main purpose is to connect to a redis server and do some unit tests.

└── src
├── main
│   ├── java
│   │   └── com
│   │   └── bswen
│   │   └── app10
│   │   ├── RedisApplication.java
│   │   ├── config
│   │   │   └── RedisConfiguration.java
│   │   ├── domain
│   │   │   └── SyncCommandCallback.java
│   │   └── service
│   │   └── RedisService.java
│   └── resources
│   ├── application-dev.properties
│   └── application.properties
└── test
├── java
│   └── com
│   └── bswen
│   └── app10
│   ├── package-info.java
│   └── service
│   └── TestRedisService.java
└── resources
└── application-test.properties

3.2 The project’s dependency

Let’s see the project’s gradle dependencies:

plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation('org.springframework.boot:spring-boot-starter-data-redis')
implementation 'io.lettuce:lettuce-core'
implementation 'org.apache.commons:commons-pool2'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
// exclude vintage, because we only use junit5
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
// specify the test profile , spring boot would try to load application-test.properties when testing
systemProperty 'spring.profiles.active', 'test'
useJUnitPlatform()
reports {
junitXml.enabled = true
html.enabled = false
}
}

You can see that we are using springboot v2.4.2, and we are using spring-boot-starter-redis with lettuce, which is the default connector. we added lettuce-core as redis connection manager.

3.3 The configuration class

We need a configuration class to configure the redis connection.

package com.bswen.app10.config;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties("spring.redis")
public class RedisConfiguration {
private String host;
private int port;
@Bean
RedisClient redisClient() {
RedisURI uri = RedisURI.Builder.redis(this.host, this.port)
.build();
return RedisClient.create(uri);
}
}

In the above code, we define two fields(host and post) to construct the redisURI , which will be used by Lettuce RedisClient to create the redis connection.

3.4 The configuration file

I have specified the test profile when doing unit testing. So, the src/test/resources/application-test.properties file’s content is as follows:

src/test/resources/application-test.properties
spring.redis.host=2.3.4.7
spring.redis.port=6379

3.5 The test class

This is the test code:

package com.bswen.app10.service;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class TestRedisService {
@Autowired
private RedisService redisService;
@Test
public void testGetAndSet() {
redisService.set("hello","world");
Assertions.assertEquals(redisService.get("hello"),"world");
}
}

I just put a key/value to redis and then get the value with the same key, the assertion should be true.

3.6 Run the code

When we run the above test with gradle, we get this exception:

Terminal window
[com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
... 122 more
Caused by: java.lang.IllegalArgumentException: Host must not be empty
at io.lettuce.core.internal.LettuceAssert.notEmpty(LettuceAssert.java:44)
at io.lettuce.core.RedisURI$Builder.redis(RedisURI.java:1151)

why?

3.7 The reason

The reason for this error is as follows:

  • The lettuce RedisURI$Builder can not find the host of the redis
  • The host is configured in our RedisConfiguration, but the host field does not have getter and setter to access it

3.8 The solution

So the solution is easy, here is it:

3.9 Re-run the test

Terminal window
Testing started at 15:31 ...
> Task :app10:cleanTest
> Task :app10:compileJava
> Task :app10:processResources UP-TO-DATE
> Task :app10:classes
> Task :app10:compileTestJava UP-TO-DATE
> Task :app10:processTestResources UP-TO-DATE
> Task :app10:testClasses UP-TO-DATE
> Task :app10:test
15:31:08.196 [Test worker] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
15:31:08.216 [Test worker] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
15:31:08.264 [Test worker] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.bswen.app10.service.TestRedisService] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
15:31:08.280 [Test worker] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.bswen.app10.service.TestRedisService], using SpringBootContextLoader
15:31:08.285 [Test worker] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.bswen.app10.service.TestRedisService]: class path resource [com/bswen/app10/service/TestRedisService-context.xml] does not exist
15:31:08.285 [Test worker] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.bswen.app10.service.TestRedisService]: class path resource [com/bswen/app10/service/TestRedisServiceContext.groovy] does not exist
15:31:08.286 [Test worker] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.bswen.app10.service.TestRedisService]: no resource found for suffixes {-context.xml, Context.groovy}.
15:31:08.287 [Test worker] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.bswen.app10.service.TestRedisService]: TestRedisService does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
15:31:08.349 [Test worker] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.bswen.app10.service.TestRedisService]
15:31:08.438 [Test worker] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [/Users/bswen/private/bw/bswen-github/bswen-springboot23/app10/build/classes/java/main/com/bswen/app10/RedisApplication.class]
15:31:08.439 [Test worker] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.bswen.app10.RedisApplication for test class com.bswen.app10.service.TestRedisService
15:31:08.577 [Test worker] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.bswen.app10.service.TestRedisService]: using defaults.
15:31:08.578 [Test worker] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
15:31:08.603 [Test worker] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@55b822bf, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@78d3e486, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@13d65ec5, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@3f6f6222, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@2b5602aa, org.springframework.test.context.support.DirtiesContextTestExecutionListener@4c462492, org.springframework.test.context.transaction.TransactionalTestExecutionListener@6d476ba9, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@29fd7a51, org.springframework.test.context.event.EventPublishingTestExecutionListener@32256e96, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@4fbaaef4, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@19769e89, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@5b9b5004, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@4253d43f, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@58040f8f, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@75ab50d0]
15:31:08.607 [Test worker] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@1e6a10f3 testClass = TestRedisService, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@1802b0a0 testClass = TestRedisService, locations = '{}', classes = '{class com.bswen.app10.RedisApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@1355ba10, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@2e7d7325, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@52bfcb96, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@ce7e970, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@25135e5, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@32d0dfaf], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
15:31:08.641 [Test worker] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)
2021-03-21 15:31:08.996 INFO 61274 --- [ Test worker] c.bswen.app10.service.TestRedisService : Starting TestRedisService using Java 1.8.0_121 on MBP-bswen with PID 61274 (started by bswen in /Users/bswen/private/bw/bswen-github/bswen-springboot23/app10)
2021-03-21 15:31:08.997 INFO 61274 --- [ Test worker] c.bswen.app10.service.TestRedisService : The following profiles are active: test
2021-03-21 15:31:09.953 INFO 61274 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-03-21 15:31:09.957 INFO 61274 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2021-03-21 15:31:09.985 INFO 61274 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 9 ms. Found 0 Redis repository interfaces.
2021-03-21 15:31:12.034 INFO 61274 --- [ Test worker] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-21 15:31:13.388 INFO 61274 --- [ Test worker] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2021-03-21 15:31:13.451 INFO 61274 --- [ Test worker] c.bswen.app10.service.TestRedisService : Started TestRedisService in 4.8 seconds (JVM running for 6.238)
2021-03-21 15:31:13.823 INFO 61274 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
BUILD SUCCESSFUL in 8s
6 actionable tasks: 3 executed, 3 up-to-date
15:31:13: Tasks execution finished ':app10:cleanTest :app10:test --tests "com.bswen.app10.service.TestRedisService"'.

It works!

4. Summary

In this post, we demonstrated how to solve the IllegalArgumentException: Host must not be empty problem when using lettuce with springboot data redis, you can see that we should provide appropriate configurations to lettuce to construct the connection from lettuce to redis server. Thanks for your reading. Regards.