I have a number of Java methods that check for a values uniqueness against similar values in a database. Heres an example:
public static boolean isNameUnique(String name){
Statement stmt;
try {
stmt = dbConnection.createStatement();
String sql = "SELECT name FROM model";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
//Retrieve by column name
String nameDatabase = rs.getString("name");
if(name.trim().equals(nameDatabase.trim())){
return false;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
However I am not sure how to test this, the project is a WAR File running on Tomcat 7. I've looked up about JUnit testing, Mockito and Arquillian but can't get a clear answer on which I should be using.
An example of a test I'd like to do is to insert a name value into the database then check the same name value to ensure false is returned for isNameUnique.
I have a number of methods similar to the one above where I require checking a value in a database against one passed in through the method.
So basically I'm looking for the most ideal way to test methods similar to this.
EDIT: Could someone explain how my question is a duplicate of the one mentioned? I don't see any similarities.
EDIT2: Sorry if its not clear, I am looking for the best way to perform a test where I insert a name value into a database and then run a method to check another name value is unique or not. Do I need a test database? Do I need to mock objects or use something like arquillian to test name uniqueness or not?