1. <small id='TaBAX'></small><noframes id='TaBAX'>

      <bdo id='TaBAX'></bdo><ul id='TaBAX'></ul>
  2. <legend id='TaBAX'><style id='TaBAX'><dir id='TaBAX'><q id='TaBAX'></q></dir></style></legend>
    <tfoot id='TaBAX'></tfoot>
  3. <i id='TaBAX'><tr id='TaBAX'><dt id='TaBAX'><q id='TaBAX'><span id='TaBAX'><b id='TaBAX'><form id='TaBAX'><ins id='TaBAX'></ins><ul id='TaBAX'></ul><sub id='TaBAX'></sub></form><legend id='TaBAX'></legend><bdo id='TaBAX'><pre id='TaBAX'><center id='TaBAX'></center></pre></bdo></b><th id='TaBAX'></th></span></q></dt></tr></i><div id='TaBAX'><tfoot id='TaBAX'></tfoot><dl id='TaBAX'><fieldset id='TaBAX'></fieldset></dl></div>

      Spring Boot 测试中的 MockBean 注解导致 NoUniqueBeanDe

      时间:2023-10-01
      <i id='vAqs6'><tr id='vAqs6'><dt id='vAqs6'><q id='vAqs6'><span id='vAqs6'><b id='vAqs6'><form id='vAqs6'><ins id='vAqs6'></ins><ul id='vAqs6'></ul><sub id='vAqs6'></sub></form><legend id='vAqs6'></legend><bdo id='vAqs6'><pre id='vAqs6'><center id='vAqs6'></center></pre></bdo></b><th id='vAqs6'></th></span></q></dt></tr></i><div id='vAqs6'><tfoot id='vAqs6'></tfoot><dl id='vAqs6'><fieldset id='vAqs6'></fieldset></dl></div>

        <small id='vAqs6'></small><noframes id='vAqs6'>

          <bdo id='vAqs6'></bdo><ul id='vAqs6'></ul>
          <tfoot id='vAqs6'></tfoot>
          1. <legend id='vAqs6'><style id='vAqs6'><dir id='vAqs6'><q id='vAqs6'></q></dir></style></legend>

                  <tbody id='vAqs6'></tbody>
                本文介绍了Spring Boot 测试中的 MockBean 注解导致 NoUniqueBeanDefinitionException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我在使用 @MockBean 注释时遇到问题.文档说 MockBean 可以替换上下文中的 bean,但我在单元测试中得到了 NoUniqueBeanDefinitionException.我看不到如何使用注释.如果我可以模拟 repo,那么显然会有不止一个 b​​ean 定义.

                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);
                }
                

                还有泽西岛资源:

                @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-SNAPSHOT2.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 的文档:

                为方便起见,需要对开始的 REST 调用的测试服务器还可以 @Autowire 一个 TestRestTemplate 这将解析到正在运行的服务器的相对链接.

                For convenience, tests that need to make REST calls to the started server can additionally @Autowire a TestRestTemplate which will resolve relative links to the running server.

                读到这里,我觉得你需要用web环境声明@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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Mockito:通缉但未调用 下一篇:测试方法之外的 Mockito 存根

                相关文章

                最新文章

                  <bdo id='LglCt'></bdo><ul id='LglCt'></ul>
                <legend id='LglCt'><style id='LglCt'><dir id='LglCt'><q id='LglCt'></q></dir></style></legend>
                <i id='LglCt'><tr id='LglCt'><dt id='LglCt'><q id='LglCt'><span id='LglCt'><b id='LglCt'><form id='LglCt'><ins id='LglCt'></ins><ul id='LglCt'></ul><sub id='LglCt'></sub></form><legend id='LglCt'></legend><bdo id='LglCt'><pre id='LglCt'><center id='LglCt'></center></pre></bdo></b><th id='LglCt'></th></span></q></dt></tr></i><div id='LglCt'><tfoot id='LglCt'></tfoot><dl id='LglCt'><fieldset id='LglCt'></fieldset></dl></div>

                <small id='LglCt'></small><noframes id='LglCt'>

                  1. <tfoot id='LglCt'></tfoot>