我有两个测试单元,一个是在数据库中插入记录,第二个是从数据库中删除记录 注意:我使用的是oracle 11g快递版。
我运行插入测试单元,它按预期工作,它在数据库中插入一条记录,然后我运行第二个测试单元并从数据库中删除记录。
现在,当我再次运行插入测试单元时,当我用我刚插入的I&#39d调用searchById方法时,它不会在部件中失败,什么是这个beahaviour
注意:我将数字3作为参数传递,因为我的表默认有两个记录。
以下是单元测试的代码:
@Test
public void insertUser()
{
userDAO.insertUser();
User testUser = userDAO.searchUserById(3);
assertNotNull(testUser);
}
@Test
public void deleteUser()
{
userDAO.deleteUserById(3);
User nullUser = userDAO.searchUserById(3);
assertNull(nullUser);
}
以下是方法的代码:
这是searchById方法:
@Override
public User searchUserById(int userId)
{
User user= null;
Connection connection = null;
try {
String storedProcedure = "{ call searchUserById(?, ?, ?, ?, ?) }";
connection = jdbcTemplate.getDataSource().getConnection();
CallableStatement callableStatement = connection.prepareCall(storedProcedureInfoUsuario);
callableStatement.setInt(1, userId);
callableStatement.registerOutParameter(2, Types.VARCHAR);
callableStatement.registerOutParameter(3, Types.VARCHAR);
callableStatement.registerOutParameter(4, Types.VARCHAR);
callableStatement.registerOutParameter(5, Types.VARCHAR);
callableStatement.executeQuery();
//
user= new User();
usuario.setName(callableStatement.getString(2));
usuario.setLastName(callableStatement.getString(3));
usuario.setEmail(callableStatement.getString(4));
usuario.setState(callableStatement.getString(5));
}
catch (SQLException ex)
{
//
ex.printStackTrace();
}
finally
{
if(connection != null)
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
return user;
}
这是插入方法:
@Override
public User insertUser(User user)
{
Connection connection = null;
try {
String storedProcedure = "{ call insertUser(?, ?, ?, ?, ?) }";
connection = jdbcTemplate.getDataSource().getConnection();
CallableStatement callableStatement = connection.prepareCall(storedProcedureInfoUsuario);
callableStatement.setInt(1, userId);
callableStatement.setString(2, user.getName());
callableStatement.setString(3, user.getLastName());
callableStatement.setString(4, user.getEmail());
callableStatement.setString(5, user.getState());
callableStatement.executeQuery();
//
user= new User();
usuario.setName(callableStatement.getString(2));
usuario.setLastName(callableStatement.getString(3));
usuario.setEmail(callableStatement.getString(4));
usuario.setState(callableStatement.getString(5));
}
catch (SQLException ex)
{
//
ex.printStackTrace();
}
finally
{
if(connection != null)
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
return user;
}
这是删除方法:
@Override
public void deleteUserById(int idUser)
{
Connection connection = null;
try {
String storedProcedure = "{ call deleteUserById(?) }";
connection = jdbcTemplate.getDataSource().getConnection();
CallableStatement callableStatement = connection.prepareCall(storedProcedureBorrarUSuario);
callableStatement.setInt(1, idUser);
callableStatement.executeQuery();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
finally
{
if(connection != null)
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
以下是存储过程 searchUserById :
CREATE OR REPLACE PROCEDURE searchUserById
(
p_userId IN User.user_id%TYPE,
ps_name OUT User.name%TYPE,
ps_lastName OUT User.lastName%TYPE,
ps_email OUT User.email%TYPE,
ps_state OUT User.state%TYPE
)
IS
BEGIN
SELECT name, lastName, email, state
INTO ps_name , ps_lastName , ps_email , ps_state
FROM USER WHERE user_id= p_userid;
END;
/
这里是插入pl sql
CREATE OR REPLACE PROCEDURE insertUser
(
ps_name IN User.name%TYPE,
ps_lastName IN User.lastName%TYPE,
ps_email IN User.email%TYPE,
ps_state IN User.state%TYPE
)
IS
BEGIN
Insert into User
(userId,name, lastname, email, state)
Values
(user_sq.nextval(), ps_name, ps_lastName , ps_email , ps_state )
COMMIT;
END;
/
这是我的删除PL / SQL
CREATE OR REPLACE PROCEDURE deleteUserById
(
p_userId IN USER.user_ID%TYPE
)
IS
BEGIN
DELETE FROM USER
WHERE user_id=p_userId;
COMMIT;
END;
导致这种行为的原因是,我单独运行它们并且两种方法都有效,就是当我第二次运行插入测试时它在搜索方式中返回null时,为什么该方法返回null是我刚插入一条记录,为什么这个方法第一次运行但不是第二次运行删除测试后
以下是我的问题的工作流程:
1) I run the insert test,
2) I check that the user was inserted
3) I run my delete test,
4) I check that the user was deleted from the table
5) I run again my test method and it fails, it can't find the user that was inserted in the code line above by the insertUser method
注意所有单独运行的方法,如果我想让测试在它们停止工作后第一次工作,我必须删除表中的所有记录并通过插入脚本再次插入它在sqlplus控制台中运行。