简体   繁体   English

如何在Spring项目中为实体类和DTO自动生成Junit测试用例

[英]How to auto generate Junit test cases for Entity classes and DTO's in spring project

I'm working on a spring boot project, i have many Entity classes and DTO classes with mostly getters and setters in it. 我正在做一个春季启动项目,我有很多Entity类和DTO类,其中大多数是getter和setter的。

is there any way or plugin that create junit test cases for all the Entity and DTO classes. 是否有任何方法或插件可以为所有Entity和DTO类创建junit测试用例。 Just to improve test coverage. 只是为了提高测试覆盖率。

currently using this frameworks Spring boot, Hibernate, Junit 4.12, Mockito. 当前使用的框架是Spring boot,Hibernate,Junit 4.12,Mockito。 Java 1.8, intellij IDE. Java 1.8,IntelliJ IDE。 EX: ` 例如:

@Entity
@Table(name = "xyz")
public class Xyz {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    public Integer id;

    @Column(name = "col1")
    public Integer col1;

    @Column(name = "col2")
    public Integer co2;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    };`

Never ever write tests for entities and DTOs. 永远不要为实体和DTO编写测试。 Usually They do not have any business logic you could test (unless something is really wrong with your design). 通常,它们没有您可以测试的任何业务逻辑(除非您的设计确实有问题)。

Better to exclude them from your test metrics than trying to make tests for making tests. 与尝试为进行测试而进行的测试相比,将它们从测试指标中排除会更好。

For test case coverage you can do it like below , But I don't think so its good way, But it helps to increase code coverage: 对于测试用例,您可以像下面这样进行,但是我不认为这是一个好方法,但是它有助于增加代码覆盖率:

public class XyzTest {

    public Xyz crateTestSuite(){
      return new Xyz();
     }

    @Test
    public void testGetId() {
     Integer id= 0;
     Xyz xyz =null;
     xyz = crateTestSuite();
     id = xyz.getId()

    }

    @Test
    public void setId(Integer id) {
     Integer id= 0;
     Xyz xyz =null;
     xyz = crateTestSuite();
     xyz.setId(id)
    } 
}

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

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