带有字符串参数的Junit测试
我是一个初学者,我正在写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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将需要向代码覆盖范围提供商暗示某些代码已被淘汰,应从覆盖范围中排除,Lombok可以通过添加生成的注释来做到这一点;如果您配置
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