简体   繁体   中英

how to String not null check in JUNIT

I am writing unit tests for he below code. but coverage is missing for the below lines of code. I am not sure how can we cover the below lines.My research didnt help.

public DetailsResponse mapRow(ResultSet resultSet, int num) throws SQLException {
    DetailsResponse DetailsResponse = new DetailsResponse();
    String[] responseElements = null;
    String response = resultSet.getString(1);
    //coverage missing for below line
    if (response != null && response.indexOf(",") != -1) {
        responseElements = response.split(",");
    }
    //coverage missing for below line
    if (responseElements != null && responseElements.length > 0) {
      //coverage missing for below line
        String id = StringUtils.isNotBlank(responseElements[0]) ? responseElements[0].replace("(", "") : "";

The commented lines are missing from the coverage., how can i test them?

Since this is a public method and you are trying to write a unit test, not an integration test, you can simply setup a ResultSet object. In doing so, you can set the object so that both conditions will get covered.

@Test
public void test(){
   // SETUP
   ResultSet resultSet = // setup ResultSet to return what looks like a comma separated list.
   // TEST
   DetailsResponse out = service. mapRow(resultSet, someNum);
   // VERIFY / ASSERT
   // some assert(s) on out
}

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