gpt4 book ai didi

java - Mockito::如何在 Dao 中模拟 SimpleDateFormat.parse()

转载 作者:行者123 更新时间:2023-12-04 16:01:26 24 4
gpt4 key购买 nike

我是 Java/Mockito 的新手,正在尝试测试 Dao 方法,特别是捕获 ParseException 并抛出 SQLException 的异常条件。

这是 Dao 代码:

public Template saveTemplate(Template template) throws SQLException {
logger.debug("Saving template details into the db ", template.getTemplateName());

SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss");
Long date = 0L;
try {
date = dt.parse(template.getStartTime()).getTime();
} catch (ParseException e) {
throw new SQLException("Error while processing date " + template.getTemplateName());
}

Long finalDate = date;

我的策略是模拟 SimpleDateFormat.parse() 调用,以便它抛出 ParseException,但这不起作用。甚至不确定这是一个好的策略...

首先我尝试了:

@InjectMocks private SimpleDateFormat simpleDateformat;

但这不起作用,因为 SimpleDateFormat 构造函数需要一个参数,并得到错误:


org.mockito.exceptions.base.MockitoException:
无法实例化类型为“class java.text.SimpleDateFormat”的名为“simpleDateFormat”的@InjectMocks 字段。
您没有在字段声明中提供实例,所以我尝试构建实例。
但是构造函数或初始化 block 抛出异常:null

然后我尝试了这个:

@Mock
private SimpleDateFormat simpleDateFormat;
@Test(expected = SQLException.class)
public void test_save_template_date_parse_error() throws ParseException, SQLException {
initMocks(this);
Mockito.mockingDetails(simpleDateFormat);
Mockito.when(simpleDateFormat.parse(anyString(),new ParsePosition(anyInt()))).thenThrow(new ParseException(anyString(),anyInt()));

Template template = new Template();
template.setStartTime("2017-01-02 12:12:12");
template.setTemplateId(1);
given(jdbcTemplate.getJdbcOperations()).willReturn(jdbcOperations);
templateDAOImpl.saveTemplate(template);
}

由此产生的错误对我不熟练的眼睛没有帮助:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced or misused argument matcher detected here:

-> at com.macys.etap.ee.dao.TemplateDAOImplTest.test_save_template_date_parse_error(TemplateDAOImplTest.java:77)
-> at com.macys.etap.ee.dao.TemplateDAOImplTest.test_save_template_date_parse_error(TemplateDAOImplTest.java:77)

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))

This message may appear after an NullPointerException if the last matcher is returning an object
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
when(mock.get(any())); // bad use, will raise NPE
when(mock.get(anyInt())); // correct usage use

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.

那么我该如何模拟这个东西并抛出错误呢?

编辑:建议的新方法,模拟 Template.getStartTime():
@Test(预期= SQLException.class)
public void test_save_template_date_parse_error() 抛出 ParseException, SQLException {
初始化模拟(这个);
模板 templateMock = Mockito.mock(Template.class);
Mockito.when(templateMock.getStartTime()).thenReturn("无效");
Mockito.mockingDetails(templateMock.getStartTime());
模板 template = new Template();
template.setStartTime("2017-01-02 12:12:12");
template.setTemplateId(1);
给出(jdbcTemplate.getJdbcOperations()).willReturn(jdbcOperations);
//根据@Daniel Pryden 修复:现在有效
templateDAOImpl.saveTemplate(templateMock);
}

现在可以使用修复程序。

最佳答案

在我看来,你在这里甚至不需要 Mockito,你可以简单地执行以下操作:

 Template template = new Template();
template.setStartTime("THIS IS AN INVALID DATE");
template.setTemplateId(1);
templateDAOImpl.saveTemplate(template);

然后将抛出 SQLException。

关于java - Mockito::如何在 Dao 中模拟 SimpleDateFormat.parse(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50392132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com