i have two tests units one is to insert a record in a database, the second one is to delete a record from a database note: i'm using oracle 11g express edition.
i run the insert test unit and it works as expected, it insert a record in the database, then i run the second test unit and delete the record from the database.
Now when i run the insert test unit again it don't work it fails in the part when i call a searchById method with the id that i just inserted, what is casuing this beahaviour
note: i pass the number 3 as a parameter because my table have two records by default.
here is the code for the unit test
@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);
}
and here is the code of the methods
here is the searchById method
@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;
}
and here is the insert method
@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;
}
here is the delete method
@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();
}
}
}
here is the storeProcedure 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;
/
and here is the insert 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;
/
and here is my delete 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;
what is causing this behaviour, i run them separately and both methods work, is just that when i run the insert test for the second time it returns null in the search by id method, why is that method returning null is i just inserted a record, and why this method works the firts time but not the second time after i run my delete test
here is the workflow of my problem
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
note all the methods where ran separately, and if i want to make the test to work for the firts time after they stop working, i have to delete all the records in the table and insert them again by a insert script that i run in the sqlplus console
Aucun commentaire:
Enregistrer un commentaire