1

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?

olliejjc16
  • 361
  • 5
  • 20
  • *Could someone explain how my question is a duplicate ...* , sure, you say *I'd like to do is to insert a name* and you want uniqueness and the dupe target can return a count. Is that explanation enough? Is that the best you can do with a question improvement? Maybe what you ought to do is show a schema for some clarity, which the people with answers ought to request before answering the question. – Drew Oct 12 '16 at 09:27
  • Maybe I didn't clarify in my question, I'm looking for the best framework to perform testing that would insert a name value into a database and then run a test to check that if a duplicate name value was passed into the method that the method would return false. I could use junit for the return false part easily enough but the real question is how to replicate having a database with a name value inserted into it for testing purposes. – olliejjc16 Oct 12 '16 at 09:31
  • So you are asking for an off-topic resource request for a testing framework. – Drew Oct 12 '16 at 09:32
  • Basically just an idea of what test framework I should be using to test a method like this. I've been researching it and can't find a clear answer. Sorry if it's off topic here to ask a question like that I didn't realize. – olliejjc16 Oct 12 '16 at 09:36

2 Answers2

0

Use the below query

String sql = "SELECT name FROM model where name != ?";

In Question mark, pass the value that you are getting by trimming that value.

murthy
  • 202
  • 5
  • 13
0

The following uses a condition to filter record, and the try-with-resources syntax, so that the statement and result set are closed, which was not the case.

String sql = "SELECT name FROM model WHERE name = ?";
try (PreparedStatement stmt = dbConnection.prepareStatement(sql)) {
    stmt.setString(1, name.trim());
    try (ResultSet rs = stmt.executeQuery()) {
        if (rs.next()){
              return false;
        }
    }
} catch (SQLException e) {
    throw new IllegalStateException(sql, e);
}
return true;

The PreparedStatement escapes special characters like ' and prevent SQL injection.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Hi thanks for that, I'll update my code to match this, however it does not answer my question on how I would actually go about testing this type of method – olliejjc16 Oct 12 '16 at 09:23
  • The rest of the actual question is (1) make a reference implementation - terrible but complete coverage, (2) make dummy data - risking testing what is covered by the code. IMHO testing frameworks may only simplify things a bit, but not take away the work, – Joop Eggen Oct 12 '16 at 09:44