带有字符串参数的Junit测试

发布于 2025-01-23 04:57:53 字数 2008 浏览 3 评论 0原文

我是一个初学者,我正在写UNITSESTS,并且偶然发现了一些我找不到适合我需求的解决方案。

我想为此进行一些JUNIT测试。

有我的班级


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;
  }
}

我的方法和我的测试阶层


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"));
    }

}

:我的测试通过了,但我的覆盖范围是30%,您有一些更好的想法吗?

谢谢 !!

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.

There is my class with my Method


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 ?

Thank you !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温折酒 2025-01-30 04:57:53

您将需要向代码覆盖范围提供商暗示某些代码已被淘汰,应从覆盖范围中排除,Lombok可以通过添加生成的注释来做到这一点;如果您配置

lombok.addLombokGeneratedAnnotation = true

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; if you configure

lombok.addLombokGeneratedAnnotation = true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文