pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      <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="3f7b7nv" 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() 之間的區(qū)別

        Difference between @Mock, @MockBean and Mockito.mock()(@Mock、@MockBean 和 Mockito.mock() 之間的區(qū)別)
          <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="xbf3335" 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() 之間的區(qū)別的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  在創(chuàng)建測試和模擬依賴時(shí),這三種方法有什么區(qū)別?

                  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 庫并且在功能上是等效的.
                  它們?cè)试S模擬類或接口并記錄和驗(yàn)證其行為.

                  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.

                  請(qǐng)注意,要在測試執(zhí)行期間啟用 Mockito 注釋,MockitoAnnotations.initMocks(this) 靜態(tài)方法必須被調(diào)用.
                  為避免測試之間的副作用,建議在每次測試執(zhí)行之前進(jìn)行:

                  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 注釋測試類,方法是指定執(zhí)行此任務(wù)的 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{...}
                  

                  <小時(shí)>

                  Spring Boot 庫封裝了 Mockito 庫

                  這確實(shí)是一個(gè) 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,它會(huì)將其替換為 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 添加模擬的注解應(yīng)用程序上下文.

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

                  ...

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

                  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.

                  <小時(shí)>

                  何時(shí)使用經(jīng)典/純 Mockito 以及何時(shí)使用 Spring Boot 中的 @MockBean?

                  單元測試旨在獨(dú)立于其他組件來測試一個(gè)組件,并且單元測試還有一個(gè)要求:在執(zhí)行時(shí)間方面盡可能快,因?yàn)檫@些測試可能每天在開發(fā)人員機(jī)器上執(zhí)行數(shù)十次.

                  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.

                  因此,這是一個(gè)簡單的指南:

                  Consequently, here is a simple guideline :

                  當(dāng)您編寫不需要來自 Spring Boot 容器的任何依賴項(xiàng)的測試時(shí),經(jīng)典/普通的 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

                  當(dāng)我們編寫一個(gè)帶有 @WebMvcTest 注釋的測試類(網(wǎng)絡(luò)測試切片)時(shí).

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

                  Spring Boot 文檔 很好地總結(jié)了這一點(diǎn):

                  The Spring Boot documentation summarizes that very well :

                  @WebMvcTest 通常會(huì)被限制在單個(gè)控制器中并用于結(jié)合 @MockBean 提供模擬實(shí)現(xiàn)需要的合作者.

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

                  這是一個(gè)例子:

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

                  這篇關(guān)于@Mock、@MockBean 和 Mockito.mock() 之間的區(qū)別的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數(shù)溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關(guān)系嗎?)
                  How to convert Integer to int?(如何將整數(shù)轉(zhuǎn)換為整數(shù)?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內(nèi)創(chuàng)建一個(gè)隨機(jī)打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲(chǔ)為 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="51jxdhz" 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'>

                            主站蜘蛛池模板: 航空铝型材,7系铝型材挤压,硬质阳*氧化-余润铝制品 | 珠光砂保温板-一体化保温板-有釉面发泡陶瓷保温板-杭州一体化建筑材料 | 实战IT培训机构_IT培训班选大学生IT技术培训中心_中公优就业 | 通用磨耗试验机-QUV耐候试验机|久宏实业百科 | 电竞馆加盟,沈阳网吧加盟费用选择嘉棋电竞_售后服务一体化 | 除甲醛公司-甲醛检测-广西雅居环境科技有限公司 | 菏泽知彼网络科技有限公司| 石英粉,滑石粉厂家,山东滑石粉-莱州市向阳滑石粉有限公司 | 深圳侦探联系方式_深圳小三调查取证公司_深圳小三分离机构 | 兰州UPS电源,兰州山特UPS-兰州万胜商贸 | ALC墙板_ALC轻质隔墙板_隔音防火墙板_轻质隔墙材料-湖北博悦佳 | 电伴热系统施工_仪表电伴热保温箱厂家_沃安电伴热管缆工业技术(济南)有限公司 | 铆钉机|旋铆机|东莞旋铆机厂家|鸿佰专业生产气压/油压/自动铆钉机 | 艺术生文化课培训|艺术生文化课辅导冲刺-济南启迪学校 | 超声波流量计_流量标准装置生产厂家 _河南盛天精密测控 | 广州二手电缆线回收,旧电缆回收,广州铜线回收-广东益福电缆线回收公司 | 屏蔽服(500kv-超高压-特高压-电磁)-徐吉电气 | 恒温恒湿箱(药品/保健品/食品/半导体/细菌)-兰贝石(北京)科技有限公司 | 土壤墒情监测站_土壤墒情监测仪_土壤墒情监测系统_管式土壤墒情站-山东风途物联网 | 危废处理系统,水泥厂DCS集散控制系统,石灰窑设备自动化控制系统-淄博正展工控设备 | 高低温试验箱-模拟高低温试验箱订制-北京普桑达仪器科技有限公司【官网】 | 杭州实验室尾气处理_实验台_实验室家具_杭州秋叶实验设备有限公司 | 儋州在线-儋州招聘找工作、找房子、找对象,儋州综合生活信息门户! | LZ-373测厚仪-华瑞VOC气体检测仪-个人有毒气体检测仪-厂家-深圳市深博瑞仪器仪表有限公司 | 无菌检查集菌仪,微生物限度仪器-苏州长留仪器百科 | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-北京罗伦过滤技术集团有限公司 | Honsberg流量计-Greisinger真空表-气压计-上海欧臻机电设备有限公司 | 高铝砖-高铝耐火球-高铝耐火砖生产厂家-价格【荣盛耐材】 | 琉璃瓦-琉璃瓦厂家-安徽盛阳新型建材科技有限公司 | 耐火浇注料-喷涂料-浇注料生产厂家_郑州市元领耐火材料有限公司 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | ?水马注水围挡_塑料注水围挡_防撞桶-常州瑞轩水马注水围挡有限公司 | 粘弹体防腐胶带,聚丙烯防腐胶带-全民塑胶 | PC构件-PC预制构件-构件设计-建筑预制构件-PC构件厂-锦萧新材料科技(浙江)股份有限公司 | 德国EA可编程直流电源_电子负载,中国台湾固纬直流电源_交流电源-苏州展文电子科技有限公司 | 气体检测仪-氢气检测仪-可燃气体传感器-恶臭电子鼻-深国安电子 | 安规电容|薄膜电容|陶瓷电容|智旭JEC安规电容厂家 | 品牌设计_VI设计_电影海报设计_包装设计_LOGO设计-Bacross新越品牌顾问 | 天津暖气片厂家_钢制散热器_天津铜铝复合暖气片_维尼罗散热器 | 办公室装修_上海办公室设计装修_时尚办公新主张-后街印象 | 上海logo设计| 全自动包装机_灌装机生产厂家-迈驰包装设备有限公司 |