使用WebMvcTest测试具有重复名称的控制器

huangapple 未分类评论47阅读模式
标题翻译

Testing a controller with duplicate name using WebMvcTest

问题

我有两个名称相同的控制器,分别位于两个不同的包中:

```java
package1.MyController
package2.MyController

我想要测试位于 package1 的控制器,使用 WebMvcTest。
因此,我在 @WebMvcTest 注解中指定了控制器的名称和包,如下所示:

@WebMvcTest(value = package1.MyController.class)
(我假设 Spring 会知道我想要测试哪个控制器)

但是我遇到了以下错误:
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'MyController' for bean class [package1.MyController] conflicts with existing, non-compatible bean definition of same name and class [package2.MyController]

有人可以向我解释这是什么原因吗?
还有没有办法告诉 Spring 我想要测试哪个控制器类(除了给它们分配不同的名称,当然)?在每个控制器上添加不同的 bean 名称似乎是无用的,因为 WebMvcTest 上的参数只接受类名。


<details>
<summary>英文翻译</summary>

I have 2 controllers wih the same name, in two different packages :

package1.MyController
package2.MyController


I want to test the controller in package1, using WebMvcTest.
So I specify the name and package of the controller in the @WebMvcTest annotation, like this :

`@WebMvcTest(value = package1.MyController.class)`
(I assume Spring will know which controller i want to test)

But I get the following error :
`Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name &#39;MyController&#39; for bean class [package1.MyController] conflicts with existing, non-compatible bean definition of same name and class [package2.MyController]`

Can someone explain that to me? 
And is there a way to tell Spring which controller class I want to test (except giving them different names, of course)? Adding a different bean name on each controller seams useless because the parameter on WebMvcTest only accepts class names.


</details>


huangapple
  • 本文由 发表于 2020年3月17日 01:45:56
  • 转载请务必保留本文链接:https://java.coder-hub.com/60710784.html
匿名

发表评论

匿名网友

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

确定