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

      1. <small id='3o4ZR'></small><noframes id='3o4ZR'>

          <bdo id='3o4ZR'></bdo><ul id='3o4ZR'></ul>
      2. <tfoot id='3o4ZR'></tfoot>
      3. Mockito - 注入模拟列表

        时间:2023-09-25

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

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

            • <small id='CnL2L'></small><noframes id='CnL2L'>

            • <tfoot id='CnL2L'></tfoot>
                  本文介绍了Mockito - 注入模拟列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下代码:

                  @Component 
                  public class Wrapper
                  { 
                      @Resource 
                      private List<Strategy> strategies;
                  
                      public String getName(String id)
                      {
                      // the revelant part of this statement is that I would like to iterate over "strategies"
                          return strategies.stream()
                              .filter(strategy -> strategy.isApplicable(id))
                              .findFirst().get().getAmount(id);
                      } 
                  }
                  

                  <小时>

                  @Component 
                  public class StrategyA implements Strategy{...}
                  
                  @Component 
                  public class StrategyB implements Strategy{...}
                  

                  我想使用 Mockito 为其创建一个测试.我写的测试如下:

                  I would like to create a Test for it using Mockito. I wrote the test as follows:

                  @InjectMocks
                  private Wrapper testedObject = new Wrapper ();
                  
                  // I was hoping that this list will contain both strategies: strategyA and strategyB
                  @Mock
                  private List<Strategy> strategies;
                  
                  @Mock
                  StrategyA strategyA;
                  
                  @Mock
                  StrategyB strategyB;
                  
                  @Test
                  public void shouldReturnNameForGivenId()
                  {   // irrevelant code...
                      //when
                      testedObject.getName(ID);
                  }
                  

                  我在线收到 NullPointerException:

                  I am getting NullPointerException on line:

                  filter(strategy -> strategy.isApplicable(id))
                  

                  ,它表示策略"列表已初始化但为空.Mohito 有没有办法像 Spring 一样表现得一样?将所有实现接口策略"的实例自动添加到列表中?

                  , which states that the "strategies" list is initialized but is empty. Is there any way Mohito will behave in the same wasy as Spring? Adding automatically all instances implementing interface "Strategy" to the list?

                  顺便说一句,我在 Wrapper 类中没有任何设置器,如果可能的话,我想以这种方式保留它.

                  Btw I do not have any setters in Wrapper class and I would like to leave it in that way if possible.

                  推荐答案

                  Mockito 无法知道您想将某些东西放入 List strategies.

                  Mockito can not know that you want to put somthing in the List strategies.

                  你应该重新考虑这个并做这样的事情

                  You should rethink this an do something like this

                  @InjectMocks
                  private Wrapper testedObject = new Wrapper ();
                  
                  private List<Strategy> mockedStrategies;
                  
                  @Mock
                  StrategyA strategyA;
                  
                  @Mock
                  StrategyB strategyB;
                  
                  @Before
                  public void setup() throws Exception {
                      mockedStrategies = Arrays.asList(strategyA, strategyB);
                      wrapper.setStrategies(mockedStrategies);
                  }
                  

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

                  上一篇:在同一个对象上多次调用 Mockito.when? 下一篇:待测类中的模拟类

                  相关文章

                  最新文章

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

                    <tfoot id='qKa75'></tfoot>

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