简体   繁体   English

如何在JUNIT中将字符串不为空检查

[英]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. 由于这是一种公共方法,并且您尝试编写单元测试而不是集成测试,因此可以简单地设置一个ResultSet对象。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM