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

      <legend id='cTEhe'><style id='cTEhe'><dir id='cTEhe'><q id='cTEhe'></q></dir></style></legend>

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

      <tfoot id='cTEhe'></tfoot>
      <i id='cTEhe'><tr id='cTEhe'><dt id='cTEhe'><q id='cTEhe'><span id='cTEhe'><b id='cTEhe'><form id='cTEhe'><ins id='cTEhe'></ins><ul id='cTEhe'></ul><sub id='cTEhe'></sub></form><legend id='cTEhe'></legend><bdo id='cTEhe'><pre id='cTEhe'><center id='cTEhe'></center></pre></bdo></b><th id='cTEhe'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='cTEhe'><tfoot id='cTEhe'></tfoot><dl id='cTEhe'><fieldset id='cTEhe'></fieldset></dl></div>
        • <bdo id='cTEhe'></bdo><ul id='cTEhe'></ul>
      1. @Mock、@MockBean 和 Mockito.mock() 之間的區別

        Difference between @Mock, @MockBean and Mockito.mock()(@Mock、@MockBean 和 Mockito.mock() 之間的區別)
          <tfoot id='Bu9CW'></tfoot>
            <bdo id='Bu9CW'></bdo><ul id='Bu9CW'></ul>
                <tbody id='Bu9CW'></tbody>
                <i id='Bu9CW'><tr id='Bu9CW'><dt id='Bu9CW'><q id='Bu9CW'><span id='Bu9CW'><b id='Bu9CW'><form id='Bu9CW'><ins id='Bu9CW'></ins><ul id='Bu9CW'></ul><sub id='Bu9CW'></sub></form><legend id='Bu9CW'></legend><bdo id='Bu9CW'><pre id='Bu9CW'><center id='Bu9CW'></center></pre></bdo></b><th id='Bu9CW'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Bu9CW'><tfoot id='Bu9CW'></tfoot><dl id='Bu9CW'><fieldset id='Bu9CW'></fieldset></dl></div>
                <legend id='Bu9CW'><style id='Bu9CW'><dir id='Bu9CW'><q id='Bu9CW'></q></dir></style></legend>

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

                1. 本文介紹了@Mock、@MockBean 和 Mockito.mock() 之間的區別的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  在創建測試和模擬依賴時,這三種方法有什么區別?

                  When creating tests and mocking dependencies, what is the difference between these three approaches?

                  1. @MockBean:

                  1. @MockBean:

                  @MockBean
                  MyService myservice;
                  

                2. @Mock:

                3. @Mock:

                  @Mock
                  MyService myservice;
                  

                4. Mockito.mock()

                5. Mockito.mock()

                  MyService myservice = Mockito.mock(MyService.class);
                  

                6. 推薦答案

                  Plain Mockito 庫

                  import org.mockito.Mock;
                  ...
                  @Mock
                  MyService myservice;
                  

                  import org.mockito.Mockito;
                  ...
                  MyService myservice = Mockito.mock(MyService.class);
                  

                  來自 Mockito 庫并且在功能上是等效的.
                  它們允許模擬類或接口并記錄和驗證其行為.

                  come from the Mockito library and are functionally equivalent.
                  They allow to mock a class or an interface and to record and verify behaviors on it.

                  使用注釋的方式更短,因此更可取,而且通常更受歡迎.

                  The way using annotation is shorter, so preferable and often preferred.

                  請注意,要在測試執行期間啟用 Mockito 注釋,MockitoAnnotations.initMocks(this) 靜態方法必須被調用.
                  為避免測試之間的副作用,建議在每次測試執行之前進行:

                  Note that to enable Mockito annotations during test executions, the MockitoAnnotations.initMocks(this) static method has to be called.
                  To avoid side effect between tests, it is advised to do it before each test execution :

                  @Before 
                  public void initMocks() {
                      MockitoAnnotations.initMocks(this);
                  }
                  

                  啟用 Mockito 注釋的另一種方法是使用 @RunWith 注釋測試類,方法是指定執行此任務的 MockitoJUnitRunner 以及其他有用的東西:

                  Another way to enable Mockito annotations is annotating the test class with @RunWith by specifying the MockitoJUnitRunner that does this task and also other useful things :

                  @RunWith(org.mockito.runners.MockitoJUnitRunner.class)
                  public MyClassTest{...}
                  

                  <小時>

                  Spring Boot 庫封裝了 Mockito 庫

                  這確實是一個 Spring Boot 類:

                  import org.springframework.boot.test.mock.mockito.MockBean;
                  ...
                  @MockBean
                  MyService myservice;
                  

                  該類包含在 spring-boot-test 庫中.

                  The class is included in the spring-boot-test library.

                  它允許在 Spring ApplicationContext 中添加 Mockito 模擬.
                  如果上下文中存在與聲明的類兼容的 bean,它會將其替換為 mock.
                  如果不是這種情況,它將在上下文中添加模擬作為 bean.

                  It allows to add Mockito mocks in a Spring ApplicationContext.
                  If a bean, compatible with the declared class exists in the context, it replaces it by the mock.
                  If it is not the case, it adds the mock in the context as a bean.

                  Javadoc 參考:

                  Javadoc reference :

                  可用于向 Spring 添加模擬的注解應用程序上下文.

                  Annotation that can be used to add mocks to a Spring ApplicationContext.

                  ...

                  如果上下文中定義了任何現有的相同類型的單個 bean將被模擬替換,如果沒有現有的 bean 被定義一個新的將被添加.

                  If any existing single bean of the same type defined in the context will be replaced by the mock, if no existing bean is defined a new one will be added.

                  <小時>

                  何時使用經典/純 Mockito 以及何時使用 Spring Boot 中的 @MockBean?

                  單元測試旨在獨立于其他組件來測試一個組件,并且單元測試還有一個要求:在執行時間方面盡可能快,因為這些測試可能每天在開發人員機器上執行數十次.

                  Unit tests are designed to test a component in isolation from other components and unit tests have also a requirement : being as fast as possible in terms of execution time as these tests may be executed each day dozen times on the developer machines.

                  因此,這是一個簡單的指南:

                  Consequently, here is a simple guideline :

                  當您編寫不需要來自 Spring Boot 容器的任何依賴項的測試時,經典/普通的 Mockito 是遵循的方式:它速度快并且有利于被測試組件的隔離.
                  如果您的測試需要依賴 Spring Boot 容器并且您還想添加或模擬容器 bean 之一:Spring Boot 中的 @MockBean 就是這樣.

                  As you write a test that doesn't need any dependencies from the Spring Boot container, the classic/plain Mockito is the way to follow : it is fast and favors the isolation of the tested component.
                  If your test needs to rely on the Spring Boot container and you want also to add or mock one of the container beans : @MockBean from Spring Boot is the way.

                  Spring Boot的典型用法@MockBean

                  當我們編寫一個帶有 @WebMvcTest 注釋的測試類(網絡測試切片)時.

                  As we write a test class annotated with @WebMvcTest (web test slice).

                  Spring Boot 文檔 很好地總結了這一點:

                  The Spring Boot documentation summarizes that very well :

                  @WebMvcTest 通常會被限制在單個控制器中并用于結合 @MockBean 提供模擬實現需要的合作者.

                  Often @WebMvcTest will be limited to a single controller and used in combination with @MockBean to provide mock implementations for required collaborators.

                  這是一個例子:

                  import org.junit.Test;
                  import org.junit.runner.RunWith;
                  import org.mockito.Mockito;
                  import org.springframework.beans.factory.annotation.Autowired;
                  import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
                  import org.springframework.boot.test.mock.mockito.MockBean;
                  import org.springframework.http.MediaType;
                  import org.springframework.test.context.junit4.SpringRunner;
                  import org.springframework.test.web.servlet.MockMvc;
                  
                  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
                  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
                  
                  @RunWith(SpringRunner.class)
                  @WebMvcTest(FooController.class)
                  public class FooControllerTest {
                  
                      @Autowired
                      private MockMvc mvc;
                  
                      @MockBean
                      private FooService fooServiceMock;
                  
                      @Test
                      public void testExample() throws Exception {
                           Foo mockedFoo = new Foo("one", "two");
                  
                           Mockito.when(fooServiceMock.get(1))
                                  .thenReturn(mockedFoo);
                  
                           mvc.perform(get("foos/1")
                              .accept(MediaType.TEXT_PLAIN))
                              .andExpect(status().isOk())
                              .andExpect(content().string("one two"));
                      }
                  
                  }
                  

                  這篇關于@Mock、@MockBean 和 Mockito.mock() 之間的區別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)

                    <tbody id='i65hj'></tbody>

                      1. <legend id='i65hj'><style id='i65hj'><dir id='i65hj'><q id='i65hj'></q></dir></style></legend>

                          • <bdo id='i65hj'></bdo><ul id='i65hj'></ul>
                            <i id='i65hj'><tr id='i65hj'><dt id='i65hj'><q id='i65hj'><span id='i65hj'><b id='i65hj'><form id='i65hj'><ins id='i65hj'></ins><ul id='i65hj'></ul><sub id='i65hj'></sub></form><legend id='i65hj'></legend><bdo id='i65hj'><pre id='i65hj'><center id='i65hj'></center></pre></bdo></b><th id='i65hj'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='i65hj'><tfoot id='i65hj'></tfoot><dl id='i65hj'><fieldset id='i65hj'></fieldset></dl></div>
                            <tfoot id='i65hj'></tfoot>
                          • <small id='i65hj'></small><noframes id='i65hj'>

                            主站蜘蛛池模板: 嫩呦国产一区二区三区av | 国产日韩av一区二区 | 午夜精品一区二区三区免费视频 | 色一情一乱一伦一区二区三区 | 成人国产精品一级毛片视频毛片 | 毛片一级片 | 欧美日本韩国一区二区 | 国产精品福利网站 | 中文字幕乱码一区二区三区 | 久久亚洲综合 | av一级| 午夜成人免费视频 | 天天干国产 | 91大神xh98xh系列全部 | 伊人伊人 | 久色视频在线 | 久久久久9999| 成人在线视| 黄色香蕉视频在线观看 | 久久999| 欧美性生活免费 | 欧美日韩国产精品一区二区 | 欧美国产日韩一区二区三区 | 日本高清视频在线播放 | av成人在线观看 | 亚洲综合在 | 国产精品无码久久久久 | 亚洲自拍偷拍免费视频 | 人成在线 | 狠狠婷婷综合久久久久久妖精 | 久久综合狠狠综合久久 | 日韩免费高清视频 | 成人在线观看免费 | 欧美一区二区久久 | 国产精品九九九 | 国产高清在线 | 在线播放一区二区三区 | 中文字幕视频在线观看 | 国产a视频 | 亚洲伊人久久综合 | 欧美一区二区三区四区在线 |