Mockito在存根化时出现NullInsteadOfMockException异常。

huangapple go评论153阅读模式
英文:

mockito NullInsteadOfMockException while stubbing

问题

以下是代码部分的中文翻译:

我想要模拟一个使用 JDBC 的 DAO 类的方法调用

    public class PointIssuanceLookupDao {
        public PointIssuanceLookupDao(@Qualifier("azureNamedParameterJdbcTemplate") NamedParameterJdbcTemplate azureNamedParameterJdbcTemplate) {
            this.azureNamedParameterJdbcTemplate = azureNamedParameterJdbcTemplate;
        }
        ...

    public CalcConstants retrieveCalcConstants (String productCode, String productSubCode){
        ....
        queryResultList = azureNamedParameterJdbcTemplate.query(PRODUCT_RECON_DETAIL_SELECT, paramMap, queryValues);
        ....

这是我的测试用例

    class PointIssuanceLookupDaoTest {

        @Mock
        NamedParameterJdbcTemplate template;

        private PointIssuanceLookupDao sut;

        @Before
        public void setUp() {
            MockitoAnnotations.openMocks(this);
            sut = new PointIssuanceLookupDao(template);
        }
    
        @Test
        void retrieveCalcConstants() {
            SqlParameterSource paramMap = new MapSqlParameterSource();
            List<Mapper> queryResultList = new ArrayList<>();

            doReturn(queryResultList).when(template).query(Mockito.anyString(), Mockito.any(MapSqlParameterSource.class), ArgumentMatchers.<RowMapper<Mapper>>any());
            ...

我在 stubbing NamedParameterJdbcTemplate 时遇到了错误

     PointIssuanceLookupDaoTest > retrieveCalcConstants() FAILED
        org.mockito.exceptions.misusing.NullInsteadOfMockException: 
         Argument passed to when() is null!
         Example of correct stubbing:
             doThrow(new RuntimeException()).when(mock).someMethod();
         Also, if you use @Mock annotation don't miss openMocks()
             at com.abc.api.points.repository.dao.PointIssuanceLookupDaoTest.retrieveCalcConstants(PointIssuanceLookupDaoTest.java:49)
    ...

我在 setUp() 中调用了 openMocks()为什么我仍然会收到此错误

注意:这是您提供的代码的中文翻译,不包括问题或其他额外的内容。如果您需要进一步的解释或帮助,请随时提出。

英文:

I want to mock a method call of a DAO class which use JDBC:

public class PointIssuanceLookupDao {
public PointIssuanceLookupDao(@Qualifier(&quot;azureNamedParameterJdbcTemplate&quot;) NamedParameterJdbcTemplate azureNamedParameterJdbcTemplate) {
this.azureNamedParameterJdbcTemplate = azureNamedParameterJdbcTemplate;
}
...
public CalcConstants retrieveCalcConstants (String productCode, String productSubCode){
....
queryResultList = azureNamedParameterJdbcTemplate.query(PRODUCT_RECON_DETAIL_SELECT, paramMap, queryValues);
....

This is my test case:

class PointIssuanceLookupDaoTest {
@Mock
NamedParameterJdbcTemplate template;
private PointIssuanceLookupDao sut;
@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
sut = new PointIssuanceLookupDao(template);
}
@Test
void retrieveCalcConstants() {
SqlParameterSource paramMap = new MapSqlParameterSource();
List&lt;Mapper&gt; queryResultList = new ArrayList&lt;&gt;();
doReturn(queryResultList).when(template).query(Mockito.anyString(), Mockito.any(MapSqlParameterSource.class), ArgumentMatchers.&lt;RowMapper&lt;Mapper&gt;&gt;any());
...

I got error even when I stubbing the NamedParameterJdbcTemplate:

 PointIssuanceLookupDaoTest &gt; retrieveCalcConstants() FAILED
org.mockito.exceptions.misusing.NullInsteadOfMockException: 
Argument passed to when() is null!
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();
Also, if you use @Mock annotation don&#39;t miss openMocks()
at com.abc.api.points.repository.dao.PointIssuanceLookupDaoTest.retrieveCalcConstants(PointIssuanceLookupDaoTest.java:49)
...

I called openMocks() in setUp(), why I still get this error?

答案1

得分: 1

我无法确定您正在使用的JUnit版本,但由于您正在使用Spring,您无需手动设置这些模拟对象。

如果您正在使用JUnit5,请只需在您的类上加上@ExtendWith(MockitoExtension.class)注解,然后只需在要测试的类上添加@InjectMocks。您不需要单独的设置方法。如果您正在使用JUnit4,请使用RunWith注解来设置测试运行器。只有在使用不同的测试运行器时,如SpringJUnit4ClassRunner时,才需要使用openMocks

@RunWith(MockitoJUnitRunner.class) // 如果使用Junit4
@ExtendWith(MockitoExtension.class) // 如果使用Junit5
class PointIssuanceLookupDaoTest {

  @Mock
  NamedParameterJdbcTemplate template;

  @InjectMocks
  private PointIssuanceLookupDao sut;
}
英文:

I can't tell which version of JUnit you're using, but since you're using Spring, you don't have to manually set up these mocks.

If you're using JUnit5, just annotate your class with @ExtendWith(MockitoExtension.class), then just add @InjectMocks on the class under test. You do not need to have a separate setup method. If you're using JUnit4, use the RunWith annotation to setup the runner. You should only be using openMocks if you're running with a different runner, like SpringJUnit4ClassRunner

@RunWith(MockitoJUnitRunner.class) // if Junit4
@ExtendWith(MockitoExtension.class) // if Junit5
class PointIssuanceLookupDaoTest {
@Mock
NamedParameterJdbcTemplate template;
@InjectMocks
private PointIssuanceLookupDao sut;
}

huangapple
  • 本文由 发表于 2023年6月2日 05:49:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定