简体   繁体   中英

Junit testing expected value

I have the following method to test in Junit

public static boolean saveLocationData(String student, String city, String location){
    boolean status=false;
    try {
        Connection mySqlConn = MySQLConn.getConnection();
        String mySqlQuery = "INSERT INTO locations VALUES ( ?, ?, ?)";
        PreparedStatement preparedStatement =    mySqlConn.prepareStatement(mySqlQuery);
        preparedStatement.setString(1, student);
        preparedStatement.setString(2, city); 
        preparedStatement.setString(3, location); 
        preparedStatement.execute();                                                                 
    } catch (Exception exception){
        /////
    }
    return status;
}

And the testing I have tried is:

final void  saveLOCATIONData() {                                   
    boolean validation = LocationDao.saveLocationData("student", "nyc", "usa");
    assertTrue(validation);

    //doesn’t exist
    validation = LocationDao.saveLocationData("example", "example", "example");
    assertFalse(validation);
}

When I run the test it says that the expected value should be true, instead is false, while this is the actual data that I have in the database. What can be causing the problem?

Please try below code:-

public static boolean saveLocationData(String student, String city, String location){
    boolean status=false;
    try {
        Connection mySqlConn = MySQLConn.getConnection();
        String mySqlQuery = "INSERT INTO locations VALUES ( ?, ?, ?)";
        PreparedStatement preparedStatement =    mySqlConn.prepareStatement(mySqlQuery);
        preparedStatement.setString(1, student);
        preparedStatement.setString(2, city); 
        preparedStatement.setString(3, location); 
        preparedStatement.execute();
        status=true;                                                                 
    } catch (Exception exception){
        /////
    }
    return status;
}

*************************************************
final void  saveLOCATIONData() {                                   
    boolean validation = LocationDao.saveLocationData("student", "nyc", "usa");
    assertTrue(validation);

    //doesn’t exist
    validation = LocationDao.saveLocationData("example", "example", "example");
    assertFalse(validation);
}

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