英文:
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'
而不必再添加这个(或类似的starter):
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.3.4.RELEASE'
但是当我尝试使用org.springframework.boot.test.web.client.TestRestTemplate
时,似乎该返回类型未包含在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
,否则starter的作用何在?是否有一种方法可以在不依赖于这两个starter的情况下使用TestRestTemplate
?
英文:
I'm trying to write integration tests in a separate project that does not have "web" code just tests. I'm trying to only add spring-test-starter like this:
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.3.4.RELEASE'
without having to also add this (or a similar starter)
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.3.4.RELEASE'
But it appears that when I go to use
org.springframework.boot.test.web.client.TestRestTemplate
The return type is not included in org.springframework.boot.test it's under "org.springframework.http.ResponseEntity"
public <T> ResponseEntity<T> getForEntity(String url,
Class<T> responseType,
Map<String,?> urlVariables)
throws RestClientException
I can't find anywhere that says that spring-boot-starter-test depends on say spring-boot-starter-web because otherwise what would be the point of the starter? Is there a way to use TestRestTemplate without depending on (2) starters?
答案1
得分: 0
如果你不想使用起始包,那么你需要手动为你想要的包添加依赖。比如 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>
请检查你需要的版本。
英文:
If you don't want to use the starter packages, then you need to add dependency for the packages you want manually. Like ResponseEntity
is a part of spring-web
. So you need to add spring-web
dependency to you pom.xml
or build.gradle
file.
if you only want spring-web for testing, then you can add this line to your build.gradle file.
testCompile group: 'org.springframework', name: 'spring-web', version: '5.2.9.RELEASE'
or your pom.xml file
<!-- 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>
Check the version you need.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论