Springboot tests the Redis connection, and solutions to various errors after startup

Develop a habit, like first and then watch!!!
I have been learning Redis these days. It was actually very simple, but I encountered various problems in the process of testing the connection. Record it here, I hope it can be useful to you help.

1-Class not found: “com.auguigu.gmall.GmallManageServiceApplicationTests”

This bug is actually obvious to everyone to know what this bug means, which means that our test class was not found. Here we mainly solve it by the following methods:

By checking the option in the settings:
Insert picture description here
afterwards, to be safe, we'd better click these buttons in the Maven option to install the module locally, so that the project must exist.
Insert picture description here
After the operation is over, we can find our corresponding The test class is out, but sorry, there should be such a bug in the future.

2-Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project *******

Insert picture description here

The specific meaning of this bug is that when we packaged through Maven with all our heart, our packaging operation failed because the test files in the project may be damaged.
Because we are here that the Test class may be wrong, so we can ignore the test class directly, so we It can be packaged normally, here we can check the box below, or directly check in the Maven option:
Insert picture description here
or
Insert picture description here
when we see the test button under the Maven project dimmed, it means that the test class has been skipped a.
However, the following error will appear after our test project was officially launched

3-Cannot determine embedded database driver class for database type NONE

Insert picture description here
Here is java.lang.IllegalStateException: Failed to load ApplicationContextthe error he will report after we start
Insert picture description here

When we go to check his related errors, we will find that the main error is this Cannot determine embedded database driver class for database type NONE, which means that the corresponding database driver was not found.

After Baidu, the Internet explained:因为spring boot默认会加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration类,DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean。因为工程中没有关于dataSource相关的配置信息,当spring创建dataSource bean因缺少相关的信息就会报错。

So we need to modify the annotation on the Springboot test startup class:
@SpringBootApplication(exclude=(DataSourceAutoConfiguration.class))
This can be started, but after testing by myself, the exact same error still appears. At
Insert picture description here
Insert picture description here
this time, I thought about it:

Our database driver is generally written in the application parameter file, and the application parameter file also happens to be our ApplicationContext, so it shouldn't be accidental, it should be the Springboot test startup class 没有自动加载我们的application参数文件, so we can only手动把这个文件加进去了 , here we can directly pass Add the following annotation implementation:
@PropertySource(value={"classpath:application.properties"})
so that we can start our test startup class again and find that it has been able to start normally, and has been able to connect to our Redis service normally.

But here we need to pay attention to a path problem . The classpath here has already indicated that it is under the resources folder, so the path of our application file only needs to write the path under resources.
Insert picture description here
And after testing by ourselves, I found that after importing If the ApplicationContext fails, you do not need to perform this step: @SpringBootApplication(exclude=(DataSourceAutoConfiguration.class))

You can manually add the application.properties file directly and it will run successfully.
Insert picture description here

If you think it is helpful to you, you can pay attention to my official account. Newcomers need your support!!!

Insert picture description here
If you don't watch it, you will look good, but if you watch it, you will look
better!

Guess you like

Origin blog.csdn.net/lovely__RR/article/details/110387272