In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about the solution of the test error report when the high version of SpringBoot is modified to the low version. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
The test class reports an error when the high version is changed to the lower version
Sometimes when you use idea to create a project through Spring Initailizr, only the most recent version of the SpringBoot project can be created by default.
If you want to change to a version, you can modify the version directly in the pom file after the project has been created.
As shown below
For example, when you create a project, the default version is 2.2.2 version org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE, and then we modify it to 1.5.10 lower version org.springframework.boot spring-boot-starter-parent 1.5.10.RELEASE.
At this point, you may encounter a problem, that is, in the high version, the default test class is fine to use.
Import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;@SpringBootTestclass SpringBootTestWebApplicationTests {@ Test void contextLoads () {System.out.println ("hello world");}}
However, after changing to a lower version, the test class will report an error, as shown below, and the org.junit.jupiter.api.Test class used in the higher version 2.2.2 cannot be imported.
At this point, you can make the following changes: 1. Delete the org.junit.jupiter.api.Test class imported by default in the higher version.
Re-import the org.junit.Test class
2. Add a comment @ RunWith (SpringRunner.class) to the class
As shown below:
Note:
With the @ RunWith annotation, change the test runner and change the test class used to SpringRunner.class to adapt to spring.
@ RunWith (SpringRunner.class) uses Spring's SpringRunner to automatically create the Spring's application context at the beginning of the test. If you want to create a spring container, you have to configure classloder for web.xml. With @ RunWith annotated, you can directly use the spring container. You can directly use the @ Test annotation without starting the spring container.
SpringRunner inherits SpringJUnit4ClassRunner and does not extend any functionality (you can see public final class SpringRunner extends SpringJUnit4ClassRunner in the source code); with the former, the name is short
3. Change the test class and test method to public
4. The last modified test class is as follows: package com.susu.springboot;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith (SpringRunner.class) @ SpringBootTestpublic class SpringBootTestApplicationTests {@ Testpublic void contextLoads () {System.out.println ("hello world");}}
Running result:
The above is what the solution of the test class error report is when the high version of SpringBoot is changed to the low version. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.