无法将SpringBootTest注解解析为类型

qxsslcnc  于 2024-01-05  发布在  Spring
关注(0)|答案(5)|浏览(169)

我的SpringBootTest annotation无法解析为类型。这里也有同样的问题,但似乎添加依赖项

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-test</artifactId>
  4. <version>2.0.5.RELEASE</version>
  5. <scope>test</scope>
  6. </dependency>

字符串
整个问题是我想使用@ContextConfiguration,但它已被弃用,建议使用@SpringBootTest(classes = MyMainClass.class)

hof1towb

hof1towb1#

删除<scope>后,我能够解决我的问题

  1. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-test</artifactId>
  5. <version>2.2.2.RELEASE</version>
  6. <!-- <scope>test</scope> -->
  7. </dependency>

字符串

1rhkuytd

1rhkuytd2#

尝试使用依赖2.0.3。我现在使用这个版本。

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-test</artifactId>
  4. <version>2.0.3.RELEASE</version>
  5. <!--<scope>test</scope>-->
  6. </dependency>

字符串

z6psavjg

z6psavjg3#

如果使用父启动器,则不需要在测试依赖项中定义版本,这可能会在加载时发生冲突。

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.5.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>

字符串
所以完整的pom应该是这样的。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>
  3. <groupId>com.kj</groupId>
  4. <artifactId>demo-multi-schema</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <packaging>jar</packaging>
  7. <name>demo</name>
  8. <description>Demo project for Spring Boot</description>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>2.0.5.RELEASE</version>
  13. <relativePath/> <!-- lookup parent from repository -->
  14. </parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-data-jpa</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>mysql</groupId>
  31. <artifactId>mysql-connector-java</artifactId>
  32. <scope>runtime</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-test</artifactId>
  37. <scope>test</scope>
  38. </dependency>
  39. </dependencies>
  40. <build>
  41. <plugins>
  42. <plugin>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-maven-plugin</artifactId>
  45. </plugin>
  46. </plugins>
  47. </build>
  48. </project>

展开查看全部
omvjsjqw

omvjsjqw4#

我会尝试在Spring Test class中使用@RunWith annotation和@Test,这里有一个例子:

  1. import org.junit.Test;
  2. import org.junit.runner.RunWith;
  3. import org.springframework.boot.test.context.SpringBootTest;
  4. import org.springframework.test.context.junit4.SpringRunner;
  5. @RunWith(SpringRunner.class)
  6. @SpringBootTest
  7. public class YourClassAppTest {
  8. @Test
  9. public void contextLoads() {
  10. }
  11. }

字符串
我希望它能解决这个问题

展开查看全部
uoifb46i

uoifb46i5#

感谢@kj007。如果有人在“gradle”上搜索答案,那么这行可能适合你:

  1. // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test
  2. testImplementation 'org.springframework.boot:spring-boot-starter-test:3.2.0'

字符串
“It worked on my Computer”(在我的电脑上运行)

相关问题