问题描述
我在使用@MockBean注释时遇到麻烦.文档说MockBean可以在上下文中替换bean,但是我在单元测试中得到了NoUniqueBeanDefinitionException.我看不到如何使用注释.如果我可以模拟该回购协议,那么显然将会有多个bean定义.
I am having trouble using the @MockBean annotation. The docs say MockBean can replace a bean within the context, but I am getting a NoUniqueBeanDefinitionException within my unit test. I can't see how to use the annotation. If I can mock the repo, then obviously there will be more than one bean definition.
我正在跟踪此处的示例: https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4
I am following the examples found here: https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4
我有一个mongo存储库:
I have a mongo repository:
public interface MyMongoRepository extends MongoRepository<MyDTO, String>
{
MyDTO findById(String id);
}
还有Jersey资源:
And a Jersey resource:
@Component
@Path("/createMatch")
public class Create
{
@Context
UriInfo uriInfo;
@Autowired
private MyMongoRepository repository;
@POST
@Produces(MediaType.APPLICATION_JSON)
public Response createMatch(@Context HttpServletResponse response)
{
MyDTO match = new MyDTO();
match = repository.save(match);
URI matchUri = uriInfo.getBaseUriBuilder().path(String.format("/%s/details", match.getId())).build();
return Response.created(matchUri)
.entity(new MyResponseEntity(Response.Status.CREATED, match, "Match created: " + matchUri))
.build();
}
}
还有一个JUnit测试:
And a JUnit test:
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestMocks {
@Autowired
private TestRestTemplate restTemplate;
@MockBean
private MyMongoRepository mockRepo;
@Before
public void setup()
{
MockitoAnnotations.initMocks(this);
given(this.mockRepo.findById("1234")).willReturn(
new MyDTO());
}
@Test
public void test()
{
this.restTemplate.getForEntity("/1234/details", MyResponseEntity.class);
}
}
错误消息:
Field repository in path.to.my.resources.Create required a single bean, but 2 were found:
- myMongoRepository: defined in null
- path.to.my.MyMongoRepository#0: defined by method 'createMock' in null
推荐答案
是一个错误: https://github.com/spring-projects/spring-boot/issues/6541
此修复程序在spring-data 1.0.2-SNAPSHOT
和2.0.3-SNAPSHOT
中: https://github.com/arangodb/spring-data/issues/14#issuecomment-374141173
The fix is in spring-data 1.0.2-SNAPSHOT
and 2.0.3-SNAPSHOT
: https://github.com/arangodb/spring-data/issues/14#issuecomment-374141173
如果您不使用这些版本,则可以通过声明其名称为模拟来解决该问题:
If you aren't using these version, you can work around it by declaring the mock with its name:
@MockBean(name="myMongoRepository")
private MyMongoRepository repository;
回复您的评论
来自 Spring的文档:
阅读此书,我认为您需要在网络环境中声明@SpringBootTest
:
Reading this, I think you need to declare @SpringBootTest
with a web environment:
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
如果您的Spring Boot无法启动Web环境,那么TestRestTemplate
的需求是什么.因此,我猜想spring甚至没有提供它.
If your spring boot doesn't start the web environment, then what is the need for TestRestTemplate
. Thus, I guess spring does not even make it available.
这篇关于Spring Boot测试中的MockBean注释导致NoUniqueBeanDefinitionException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!