你可以在没有 starter-web 的情况下使用 spring-boot-starter-test 吗?
Can you use spring-boot-starter-test without starter-web?
我正在尝试在一个单独的项目中编写集成测试,该项目没有“web”代码只是测试。我试图只添加 spring-test-starter 这样的:
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.3.4.RELEASE'
无需添加这个(或类似的启动器)
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.3.4.RELEASE'
但似乎当我去使用
org.springframework.boot.test.web.client.TestRestTemplate
return 类型未包含在 org.springframework.boot.test 中,它位于“org.springframework.http.ResponseEntity”
下
public <T> ResponseEntity<T> getForEntity(String url,
Class<T> responseType,
Map<String,?> urlVariables)
throws RestClientException
我找不到任何地方说 spring-boot-starter-test 取决于说 spring-boot-starter-web 否则启动器的意义何在?有没有一种方法可以在不依赖 (2) 个初学者的情况下使用 TestRestTemplate?
如果您不想使用启动包,那么您需要手动为您需要的包添加依赖。就像 ResponseEntity
是 spring-web
的一部分。所以你需要给你的pom.xml
或build.gradle
文件添加spring-web
依赖。
如果您只想 spring-web 进行测试,那么您可以将此行添加到您的 build.gradle 文件中。
testCompile group: 'org.springframework', name: 'spring-web', version: '5.2.9.RELEASE'
或您的 pom.xml 文件
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.9.RELEASE</version>
<scope>test</scope>
</dependency>
检查您需要的版本。
我正在尝试在一个单独的项目中编写集成测试,该项目没有“web”代码只是测试。我试图只添加 spring-test-starter 这样的:
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.3.4.RELEASE'
无需添加这个(或类似的启动器)
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.3.4.RELEASE'
但似乎当我去使用 org.springframework.boot.test.web.client.TestRestTemplate
return 类型未包含在 org.springframework.boot.test 中,它位于“org.springframework.http.ResponseEntity”
下public <T> ResponseEntity<T> getForEntity(String url,
Class<T> responseType,
Map<String,?> urlVariables)
throws RestClientException
我找不到任何地方说 spring-boot-starter-test 取决于说 spring-boot-starter-web 否则启动器的意义何在?有没有一种方法可以在不依赖 (2) 个初学者的情况下使用 TestRestTemplate?
如果您不想使用启动包,那么您需要手动为您需要的包添加依赖。就像 ResponseEntity
是 spring-web
的一部分。所以你需要给你的pom.xml
或build.gradle
文件添加spring-web
依赖。
如果您只想 spring-web 进行测试,那么您可以将此行添加到您的 build.gradle 文件中。
testCompile group: 'org.springframework', name: 'spring-web', version: '5.2.9.RELEASE'
或您的 pom.xml 文件
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.9.RELEASE</version>
<scope>test</scope>
</dependency>
检查您需要的版本。