• <legend id='Z5fNx'><style id='Z5fNx'><dir id='Z5fNx'><q id='Z5fNx'></q></dir></style></legend>

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

        <bdo id='Z5fNx'></bdo><ul id='Z5fNx'></ul>
    2. <i id='Z5fNx'><tr id='Z5fNx'><dt id='Z5fNx'><q id='Z5fNx'><span id='Z5fNx'><b id='Z5fNx'><form id='Z5fNx'><ins id='Z5fNx'></ins><ul id='Z5fNx'></ul><sub id='Z5fNx'></sub></form><legend id='Z5fNx'></legend><bdo id='Z5fNx'><pre id='Z5fNx'><center id='Z5fNx'></center></pre></bdo></b><th id='Z5fNx'></th></span></q></dt></tr></i><div id='Z5fNx'><tfoot id='Z5fNx'></tfoot><dl id='Z5fNx'><fieldset id='Z5fNx'></fieldset></dl></div>
      1. <small id='Z5fNx'></small><noframes id='Z5fNx'>

        使用 Mockito 模拟接口

        时间:2023-09-25
          <legend id='E4tY2'><style id='E4tY2'><dir id='E4tY2'><q id='E4tY2'></q></dir></style></legend>
          • <bdo id='E4tY2'></bdo><ul id='E4tY2'></ul>
            <tfoot id='E4tY2'></tfoot>
            <i id='E4tY2'><tr id='E4tY2'><dt id='E4tY2'><q id='E4tY2'><span id='E4tY2'><b id='E4tY2'><form id='E4tY2'><ins id='E4tY2'></ins><ul id='E4tY2'></ul><sub id='E4tY2'></sub></form><legend id='E4tY2'></legend><bdo id='E4tY2'><pre id='E4tY2'><center id='E4tY2'></center></pre></bdo></b><th id='E4tY2'></th></span></q></dt></tr></i><div id='E4tY2'><tfoot id='E4tY2'></tfoot><dl id='E4tY2'><fieldset id='E4tY2'></fieldset></dl></div>
              <tbody id='E4tY2'></tbody>
            1. <small id='E4tY2'></small><noframes id='E4tY2'>

                • 本文介绍了使用 Mockito 模拟接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有人可以帮我处理下面的 Mock 对象吗?我想为 ServiceImpl 类编写一个模拟测试用例.我想模拟 OrderIF 接口:

                  Can someone please help me with the below Mock object. I want to write a mock test case for ServiceImpl class. I want to mock OrderIF interface:

                  public interface OrderIF{
                      List<Order> ordersFor(String type);
                  }
                  

                  服务的实现是:

                  public class ServiceImpl implements Service {
                      private List <Order> orders ;
                      private OrderIF orderif ; // this is 3rd party interface
                  
                      public int getval(String type) {
                         //some code 
                  
                         // this returns a list of objects (orders)
                         orders = orderif.ordersFor(type);
                  
                         // some code 
                         return orders.get(0)
                      }
                  }
                  

                  我的代码给出 NullPoinerException:

                  My code give NullPoinerException:

                  public class ServiceImplTest {
                       private List <Order> ll ;
                       private service reqService ; 
                  
                       @InjectMocks
                       private orderIF order;
                  
                       @Before
                       public void setUp() throws Exception {
                           ll = new ArrayList<Order> ();
                           ll.add(new Order("Buy"  ,  11 , "USD" ));
                           ll.add(new Order("Sell" ,  22 , "USD" ));
                           reqService = spy(new ServiceImpl());
                       }
                  
                       @Test
                       public void test() {
                          String type= "USD" ; 
                          when(order.ordersFor(type)).thenReturn(ll);
                          q = reqService.getval(type);
                          assertTrue(q.get().ask == 232.75);
                      }
                  }
                  

                  推荐答案

                  @InjectMocks 不会实例化或模拟您的类.此注解用于将模拟注入此字段.

                  @InjectMocks will not instantiate or mock your class. This annotation is used for injecting mocks into this field.

                  如果你想测试 serviceImpl 你需要这样模拟:

                  If you want to test serviceImpl you will need to mock in this way:

                  @Mock
                  private OrderIF order;
                  
                  @InjectMocks
                  private Service reqService = new ServiceImpl(); 
                  

                  要使其工作,您需要在 @Before 方法中使用 runner 或 MockitoAnnotations.initMocks(this);.

                  To make it work you either need to use runner or MockitoAnnotations.initMocks(this); in @Before method.

                  这篇关于使用 Mockito 模拟接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:即使在添加 @PrepareForTest 之后,Mockito 也会要求为 下一篇:使用 Mockito,我如何匹配地图的键值对?

                  相关文章

                  最新文章

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

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

                    <bdo id='vwstt'></bdo><ul id='vwstt'></ul>

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