简体   繁体   English

JUNIT 带字符串参数的测试

[英]JUNIT Test with string parameters

I'm a beginner and i'm writing unittests and I've stumbled across something I can't find a solution for that fits my needs.我是初学者,我正在编写单元测试,我偶然发现了一些我找不到适合我需要的解决方案的东西。

I want to write some Junit Test for that exceptions.我想为该异常编写一些 Junit 测试。

There is my class with my Method我的方法是 class


import javax.validation.constraints.Size;

import org.springframework.validation.annotation.Validated;

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/**
 * CustomerInfo
 */
@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.SpringCodegen", date = "2022-02-21T07:51:10.135Z[GMT]")

@Data
@EqualsAndHashCode
@ToString
public class CustomerInfo   {
  @JsonProperty("LsId")
  @Size(max=40)
  private String lsId = null;

  @JsonProperty("SocialReason")
  @Size(max=100)
  private String socialReason = null;

  @JsonProperty("NationalId")
  @Size(max=40)
  private String nationalId = null;

  public CustomerInfo lsId(String lsId) {
    this.lsId = lsId;
    return this;
  }

  public CustomerInfo socialReason(String socialReason) {
    this.socialReason = socialReason;
    return this;
  }

  public CustomerInfo nationalId(String nationalId) {
    this.nationalId = nationalId;
    return this;
  }
}

and my testClass:和我的测试类:


import static org.junit.jupiter.api.Assertions.*;

import org.aspectj.lang.annotation.Before;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;

class CustomerInfoTest {
    
    
    CustomerInfo customInfo = new CustomerInfo();

    @Before(value = "")
    public void setUp() {
        customInfo.setLsId("6485");
        customInfo.setSocialReason("banque de france");
        customInfo.setNationalId("54865");
    }
    
    @Test
    void testLsId() {
        assertEquals(customInfo, customInfo.lsId("4852"));
    }

    @Test
    void testSocialReason() {
        assertEquals(customInfo, customInfo.socialReason("bank"));
    }

    @Test
    void testNationalId() {
        assertEquals(customInfo, customInfo.nationalId("4852"));
    }

}

My tests pass but my coverage is average 30% with that, do you have some better ideas?我的测试通过了,但我的覆盖率平均为 30%,你有更好的主意吗?

Thank you !!谢谢 !!

You will need to hint to the code coverage provider that some code has been geenrated and should be excluded from coverage, lombok can do this by adding a generated annotation;您将需要向代码覆盖率提供者提示某些代码已被生成并且应该被排除在覆盖范围之外,lombok 可以通过添加生成的注释来做到这一点; if you configure如果你配置

lombok.addLombokGeneratedAnnotation = true

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

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