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

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

      1. 本机查询上的 Mockito NullPointerException

        时间:2023-09-26
      2. <tfoot id='wsQo0'></tfoot>

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

              <tbody id='wsQo0'></tbody>

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

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

                <legend id='wsQo0'><style id='wsQo0'><dir id='wsQo0'><q id='wsQo0'></q></dir></style></legend>
                • 本文介绍了本机查询上的 Mockito NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我的查询对象有问题,即使我使用查询模拟对象存根它,它也会变为空.这是代码

                  I am having a problem on my query object, it becomes null even though I stub it with a query mock object.. This is the code

                  Query query = getEntityManager().createNativeQuery(queryString, SomeRandom.class);
                  
                  return query.getResultList(); //-->This is where I get the error, the query object is null.
                  

                  我的测试方法是

                  Query query = mock(Query.class);
                  when(entityManager.createNativeQuery("", SomeRandom.class)).thenReturn(query);
                  List<SomeRandom> someList = requestDao.getSomeList(parameter, parameter, parameter, parameter);
                  

                  推荐答案

                  这可能意味着您传递给模拟方法的匹配器之一不匹配.您传递了一个实际的 String 实例(空字符串),该实例在后台转换为 Equals 匹配器.只有当 queryString 也是空字符串时,您的示例才有效.

                  This probably means that one of the matchers that you passed to the mocked method did not match. You passed an actual String instance (the empty string), which is transformed under the hood into an Equals matcher. Your example would only work if queryString was the empty string as well.

                  这应该匹配任何查询字符串:

                  This should match on any query string:

                  when(entityManager.createNativeQuery(anyString(), eq(SomeRandom.class)))
                    .thenReturn(query);
                  

                  这在您希望传递的一些具体字符串上:

                  And this on some concrete String that you expect to be passed:

                  String expectedQueryString = "select 1";
                  
                  when(entityManager.createNativeQuery(expectedQueryString, SomeRandom.class))
                    .thenReturn(query);
                  

                  根据评论

                  如果从 eq(SomeRandom.class) 更改为 any() 解决了问题,那么 eq(SomeRandom.class) 匹配器不匹配,这意味着 SomeRandom.class 实际上不是传递给模拟方法的内容.

                  If changing from eq(SomeRandom.class) to any() solved the problem, then the eq(SomeRandom.class) matcher did not match, which means SomeRandom.class was not what was in fact passed to the mocked method.

                  这篇关于本机查询上的 Mockito NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用 Mockito 验证重载方法的调用次数? 下一篇:Mockito 模拟对象突然抛出 NPE

                  相关文章

                  最新文章

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

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

                      <tfoot id='CPjlH'></tfoot>