I have this issue, I'm creating a Junit test and I want something to do first with setUp method. See my sample code:
class ClassDaoImplTests {
@Autowired
private NamedParameterJdbcTemplate template;
private Project modifiedProject;
private Project origProject;
List<Project> projList;
@BeforeAll
protected void setUp() {
preserveOriginalProjectData();
prepareProjectDataToUpdate();
}
public void preserveOriginalProjectData() {
projList = getSortedProjectList();
origProject = projList.get(0);
}
public void prepareProjectDataToUpdate() {
modifiedProject = projList.get(0);
modifiedProject.setProjName(modifiedProject.getProjName() + "_Edit");
}
public List<Project> getSortedProjectList() {
String sql = "SELECT * FROM Table1 ORDER BY id ASC";
return template.query(sql, new BeanPropertyRowMapper<Project>(Project.class));
}
}
Whatever I do with the modifiedProject
, it reflects with origProject
object and also the object inside the list. Even I use this kind of approached the result is the same:
protected void setUp() {
projList = getSortedProjectList();
origProject = projList.get(0);
prepareProjectDataToUpdate();
}
How can I preserve the original object's data?
I have not included other code's to minimize the size of code. But just stating the logic.
Actually I want the origProject
data for tearDown()
to restore the values in the table.