- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
@Transactional
public Boolean save(final StudentLogEntry studentLogEntry) throws SQLException,DaoException{
boolean returnFlag = true;
String sqlInsert = "insert into STUDENT_DETAILS (INSERT_DATE,STUDENT_NAME,STUDENT_ID) values(SYSDATE,?,?)";
returnFlag = jdbcTemplate.execute(
sqlInsert,
new PreparedStatementCallback<Boolean>() {
Boolean b=false;
@Override
public Boolean doInPreparedStatement(PreparedStatement pst) {
try {
pst.setString(2, studentLogEntry.getStuName());
pst.setString(3, studentLogEntry.getStuId());
b=pst.execute();
} catch (SQLException e) {
clicklogger.info("SQLException has occurred while inserting studentlog ",e);
} catch (DataAccessException e) {
clicklogger.info("DataAccessException has occurred while inserting studentlog ",e);
}
return b;
}
}
);
return returnFlag;
}
我的项目使用Spring框架,我已经为上面的代码编写了Junit测试用例。但我无法涵盖 PreparedStatementCallback
。我的测试用例如下:
@Test
public void testSave() throws DaoException, SQLException {
studentDao.setJdbcTemplate(jdbcTemplate);
studentDao.setTransactionManager(transactionManager);
StudentLogEntry studentLogEntry = new StudentLogEntry();
studentLogEntry.getStuName("ABC");
studentLogEntry.getStuId("1234");
assertEquals("true",studentDao.save(studentLogEntry));
}
最佳答案
该方法做了很多工作,使其可以轻松、清晰地进行测试。
1) 我会将新的 PreparedStatementCallback
移至专门的类:
public class MyPreparedStatementCallback implements PreparedStatementCallback<Boolean>{
@Override
public Boolean doInPreparedStatement(PreparedStatement pst) {
....
}
我将在这里单独测试 doInPreparedStatement
方法。应该和之前的测试完全没有关系。
2) 在原始方法中使用新类并测试是否已传递正确的实例(您需要 Mockito here ):
returnFlag = jdbcTemplate.execute(sqlInsert,new MyPreparedStatementCallback());
和测试:
@InjectMocks
StudentDao studentDao;
@Mock
JdbcTemplate jdbcTemplateMock;
@Captor
ArgumentCaptor argCaptor;
@Before
public void init(){
MockitoAnnotations.initMocks(this);
}
@Test
public void shouldSaveWithCallback(){
// Act
// set up
studentDao.save(studentLogEntry);
myClass.userPressedButton();
Mockito.verify(jdbcTemplateMock).execute(Mockito.anyString(), argCaptor.capture());
// Assert
assertTrue(argCaptor.getValue() instance of MyPreparedStatementCallback);
}
底线是您不应该使用原始测试方法来测试该回调的实现。这太多了,您的测试将过于粗糙且难以维护。
关于java - PreparedStatementCallback 覆盖率的 Junit 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47650721/
@Transactional public Boolean save(final StudentLogEntry studentLogEntry) throws SQLException,Da
我一直在尝试使用 Postgres 数据库进行 Spring 安全性身份验证。 这是我的 SQL 表定义: CREATE TABLE "UTILISATEUR" ( "IdUtilisateur" s
我有一个关于PreparedStatementCallback的问题,如下所示: Error! Conflict [#409]: PreparedStatementCallback; SQL [EX
我的要求是搜索存储在 DB2 中的目的地列表,当我输入一个字符或字符列表时,我使用的是 jquery 自动完成和 ajax 调用。一旦我在目标文本框中输入一个字符或字符串,它应该通过查询 DB2 来检
我在运行 SQL 查询时遇到问题。我有多个这样的查询,但只有这个不起作用。这是我的错误: 06/16/20 19:54:05.808 INFO Camel (camel-1) thread #25
我想要 Spring Batch 元数据 在 MySQL 服务器上创建并使用所有来自 Oracle 的现有表从中获取数据并将其放入 MongoDB。 我创建了以下配置,但不知何故错过了通过配置创建 S
我正在运行一个带有 Spring BootSecurtiy 的 Spring Boot 应用程序,即身份验证管理器构建器。登录应该由数据库的实体完成。但是每当我登录时它都会显示以下错误。 Prepar
我正在开发 Spring Batch Example – CSV File To MySQL Database 示例。我开发了整个代码,但是当我运行代码时,我看到出现以下错误。 我原以为 spring
我是一名优秀的程序员,十分优秀!