简体   繁体   中英

How can I test an SQL's UPDATE operation with JUnit?

I'm using thymeleaf and JDBC. I have this method that updates a Job object which contains a query. How can I test this using JUnit?

public int updateJob(Job job){
  MapSqlParameterSource namedParameters=new MapSqlParameterSource();
  String sql1 = "UPDATE jobs SET title = :title, tool1 = :tool1 , tool2= :tool2 WHERE id = :id";  
  namedParameters.addValue("title", job.getTitle());
  namedParameters.addValue("tool1", job.getTool1());
  namedParameters.addValue("tool2", job.getTool2());
  namedParameters.addValue("id", job.getId());
  int rValue=jdbc.update(sql1, namedParameters);
  return rValue;
}

I also have the following method:

public int createJob(Job job){
  String sql1="INSERT INTO jobs (employee, title, tool1, tool2) VALUES(:employee, :title, :tool1, :tool2)";
  namedParameters.addValue("title", job.getTitle());
  namedParameters.addValue("tool1", job.getTool1());
  namedParameters.addValue("tool2", job.getTool2());
  namedParameters.addValue("id", job.getId());
  int rValue=jdbc.update(sql1, namedParameters);
  return rValue;
}

So I suppose I need to use the createJob() first, then updateJob() and somehow compare the resulting values???

What's the level of your tests?

For unit tests, if be strongly surprised, if a database was involved. The test would be to make sure that the correct update statement has been generated. If you test for actual data, it'd be hard to isolate against other test cases. And you'd unit test the database. Mocking the database can help.

For integration tests, you'd make sure that your database is in the state you expect when you execute this test. It's a matter of proper setup and tearDown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM