英文:
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("azureNamedParameterJdbcTemplate") 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<Mapper> queryResultList = new ArrayList<>();
doReturn(queryResultList).when(template).query(Mockito.anyString(), Mockito.any(MapSqlParameterSource.class), ArgumentMatchers.<RowMapper<Mapper>>any());
...
I got error even when I stubbing the 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)
...
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论