久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <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 class="qwawimqqmiuu" 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>

        Spring Boot 測試中的 MockBean 注解導致 NoUniqueBeanDe

        MockBean annotation in Spring Boot test causes NoUniqueBeanDefinitionException(Spring Boot 測試中的 MockBean 注解導致 NoUniqueBeanDefinitionException)

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

              <bdo id='TaBAX'></bdo><ul id='TaBAX'></ul>
              • <legend id='TaBAX'><style id='TaBAX'><dir id='TaBAX'><q id='TaBAX'></q></dir></style></legend>
                <tfoot id='TaBAX'></tfoot>
              • <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 class="qwawimqqmiuu" id='TaBAX'><tfoot id='TaBAX'></tfoot><dl id='TaBAX'><fieldset id='TaBAX'></fieldset></dl></div>
                  <tbody id='TaBAX'></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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關系嗎?)
                  How to convert Integer to int?(如何將整數轉換為整數?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內創建一個隨機打亂數字的 int 數組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠將 0xff000000 存儲為 int?)
                    <bdo id='LglCt'></bdo><ul id='LglCt'></ul>
                      <tbody id='LglCt'></tbody>
                    <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 class="qwawimqqmiuu" 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>
                            主站蜘蛛池模板: 九九热在线视频 | 精品日韩欧美一区二区 | 亚洲成人精品一区二区 | av天天干| 午夜黄色影院 | 成人在线免费观看 | 免费成人高清 | 日本啊v在线 | 人人种亚洲 | 亚洲国产黄色av | 99久久国产免费 | 一区在线播放 | 欧美精品1区2区 | 国产日韩一区二区 | 日本三级网站在线观看 | 欧美在线一区二区三区四区 | 亚洲网站在线观看 | 国产日韩欧美精品 | 欧美激情精品久久久久久变态 | 欧美成人a∨高清免费观看 91伊人 | 国产一级在线 | 成人免费在线观看 | 男人天堂午夜 | 国产精品揄拍一区二区 | 久久专区| 国产一区精品在线 | 久久亚洲国产精品 | 性做久久久久久免费观看欧美 | 黄片毛片 | 久久成人一区 | 精品乱码一区二区 | 欧美日韩免费一区二区三区 | www.色.com| 久久久国产一区二区三区 | 黄色亚洲 | 国产精品久久久久久久久图文区 | 涩涩视频网站在线观看 | 精品伦精品一区二区三区视频 | 91午夜在线| 亚洲精品久久久一区二区三区 | 一本久久a久久精品亚洲 |