<bdo id='sBMKu'></bdo><ul id='sBMKu'></ul>
    <legend id='sBMKu'><style id='sBMKu'><dir id='sBMKu'><q id='sBMKu'></q></dir></style></legend>

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

    <i id='sBMKu'><tr id='sBMKu'><dt id='sBMKu'><q id='sBMKu'><span id='sBMKu'><b id='sBMKu'><form id='sBMKu'><ins id='sBMKu'></ins><ul id='sBMKu'></ul><sub id='sBMKu'></sub></form><legend id='sBMKu'></legend><bdo id='sBMKu'><pre id='sBMKu'><center id='sBMKu'></center></pre></bdo></b><th id='sBMKu'></th></span></q></dt></tr></i><div id='sBMKu'><tfoot id='sBMKu'></tfoot><dl id='sBMKu'><fieldset id='sBMKu'></fieldset></dl></div>
    1. <tfoot id='sBMKu'></tfoot>
    2. Mockito + Spring + @PostConstruct,mock初始化错误,为什

      时间:2023-10-01
        • <i id='HaPbh'><tr id='HaPbh'><dt id='HaPbh'><q id='HaPbh'><span id='HaPbh'><b id='HaPbh'><form id='HaPbh'><ins id='HaPbh'></ins><ul id='HaPbh'></ul><sub id='HaPbh'></sub></form><legend id='HaPbh'></legend><bdo id='HaPbh'><pre id='HaPbh'><center id='HaPbh'></center></pre></bdo></b><th id='HaPbh'></th></span></q></dt></tr></i><div id='HaPbh'><tfoot id='HaPbh'></tfoot><dl id='HaPbh'><fieldset id='HaPbh'></fieldset></dl></div>

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

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

              <tbody id='HaPbh'></tbody>
            1. <tfoot id='HaPbh'></tfoot>

                <bdo id='HaPbh'></bdo><ul id='HaPbh'></ul>
              • 本文介绍了Mockito + Spring + @PostConstruct,mock初始化错误,为什么会调用@PostConstruct?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有这样的设置:

                Bean类:

                private final Map<String, String> configCache = new HashMap<>();
                @PostConstruct
                private void fillCache() {  (...) configCache.clear();} 
                

                TestConfig 类:

                @Bean
                @Order(Ordered.HIGHEST_PRECEDENCE)
                @Primary
                public Bean beanMock() {
                    return Mockito.mock(Bean.class);
                }
                

                Test 类:哪个 @Autowires bean.

                Test class: which @Autowires the bean.

                似乎当 Mockito 在 TestConfig 中创建模拟时,它调用了 @PostConstruct ,而 @PostConstruct 又似乎在映射字段初始化之前被调用,因此它引发了异常.

                It seems when Mockito is creating the mock in TestConfig, it calls @PostConstruct which in turn seems to be called before the map field is initialized so it throws an exception.

                我的问题是:

                • 为什么 Mockito 调用 @PostConstruct?
                • 如何禁用 @PostConstruct 进行模拟?

                显然调用是在 Spring 从 Config 的 @Bean 方法中重新调用 bean 之前的实例化之后完成的

                Apparently the call is done after the instantiation just before Spring retrns the bean from a Config's @Bean method

                推荐答案

                Mockito 没有调用 @PostConstruct -- Spring 是.你说在你的测试中你使用了 @Autowired,这不是一个 Mockito 注释.

                Mockito isn't calling @PostConstruct -- Spring is. You say that in your test you use @Autowired, which is not a Mockito annotation.

                如果您打算使用 @Mock,您会发现 Mockito 不会调用您的 @PostConstruct 方法.

                If you meant to use @Mock, you'll find that Mockito won't call your @PostConstruct method.

                换句话说,像这样编写你的测试类:

                In other words, write your test class like this:

                @Mock Bean myBean;
                
                @Before
                public void before() {
                    MockitoAnnotations.initMocks();
                }
                

                这篇关于Mockito + Spring + @PostConstruct,mock初始化错误,为什么会调用@PostConstruct?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用 Mockito 和 jUnit 模拟持久化和实体 下一篇:无法使用 Mockito 返回类对象

                相关文章

                最新文章

                  • <bdo id='PkfUg'></bdo><ul id='PkfUg'></ul>

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

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

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