你可以在没有starter-web的情况下使用spring-boot-starter-test吗?

huangapple go评论67阅读模式
英文:

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: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-starter-test&#39;, version: &#39;2.3.4.RELEASE&#39;

without having to also add this (or a similar starter)

implementation group: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-starter-web&#39;, version: &#39;2.3.4.RELEASE&#39;

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 &lt;T&gt; ResponseEntity&lt;T&gt; getForEntity(String url,
                                          Class&lt;T&gt; responseType,
                                          Map&lt;String,?&gt; 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

如果你不想使用起始包,那么你需要手动为你想要的包添加依赖。比如 ResponseEntityspring-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: &#39;org.springframework&#39;, name: &#39;spring-web&#39;, version: &#39;5.2.9.RELEASE&#39;

or your pom.xml file

&lt;!-- https://mvnrepository.com/artifact/org.springframework/spring-web --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
    &lt;version&gt;5.2.9.RELEASE&lt;/version&gt;
    &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;

Check the version you need.

huangapple
  • 本文由 发表于 2020年10月7日 22:08:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64245847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定