简体   繁体   English

如何对 JPA idclass 进行单元测试

[英]How to Unit test JPA idclass

I am struggling to write/understand a unit test for JPA inheritance @idClass in Intellij IDE.我正在努力编写/理解 Intellij IDE 中 JPA 继承 @idClass 的单元测试。 When running the test with code coverage, the IDE displays 5/6 method covered.运行代码覆盖率测试时,IDE 显示 5/6 方法覆盖。 But the class has only five methods.但是这个类只有五个方法。 Where is the 6th method?第6种方法在哪里? What am I missing?我错过了什么?

package com.beetlehand.model;

import javax.persistence.*;
import java.util.Objects;

@Entity
@Table(name = "product_attribute", schema = "beetlehand", catalog = "")
@IdClass(ProductAttributeEntityPK.class)
public class ProductAttributeEntity {
    private int productId;
    private int attributeValueId;

    /*** Getters and Setters ***/

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ProductAttributeEntity that = (ProductAttributeEntity) o;
        return productId == that.productId &&
                attributeValueId == that.attributeValueId;
    }

    @Override
    public int hashCode() {
        return Objects.hash(productId, attributeValueId);
    }
}

And the unit test和单元测试

package com.beetlehand.model;

import org.junit.jupiter.api.Test;

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

public class ProductAttributeEntityPKTest {
    @Test
    void testGetProductId() {
        ProductAttributeEntity entity = new ProductAttributeEntity();
        entity.setProductId(1);
        entity.setAttributeValueId(1);

        assertEquals(1, entity.getProductId());
    }
    @Test
    void testGetAttributeValueId() {
        /*** test logic ***/

        assertEquals(1, entity.getAttributeValueId());
    }
    @Test
    void testEquals() {
        /*** test logic ***/

        assertEquals(true, entity.equals(entity2));
    }
    @Test
    void testHashCode() {
        /*** test logic ***/

        assertEquals(entity2.hashCode(), entity.hashCode());
    }
}

I'm thinking each control in if statement increases the value of coverage .我在想if语句中的每个控件都会增加coverage的值。 You can't get a 100% covarage value because you do not provide all the controls in the equals method.您无法获得100% 的 covarage值,因为您没有在equals方法中提供所有控件。 You must provide all the controls (same object, null object, different class object) in unit test methods.您必须在单元测试方法中提供所有控件(相同对象、空对象、不同类对象)。

!!! !!! You must check null object firstly otherwise you will get an NullPointerException when you submit a null object.您必须首先检查空对象,否则在提交空对象时会得到NullPointerException

I also want to make suggestions for testing.我也想提出测试建议。 Firstly, I suggest you do research on unit test naming conventions .首先,我建议您研究单元测试命名约定 For example, you can review this title -> Unit test naming best practices例如,您可以查看此标题 -> 单元测试命名最佳实践

@Test
void equals_WhenObjectIsNull_ShouldReturnFalse() {

}

@Test
void equals_WhenObjectIsSame_ShouldReturnTrue() {

}

My other suggestion is that you can create the entity object under @Before annotation and reduce the code lines.我的另一个建议是,您可以在@Before注释下创建实体对象并减少代码行。

public ProductAttributeEntity entity;
 
@Before
public void setUp() {
    entity = new ProductAttributeEntity();
    entity.setProductId(1);
    entity.setAttributeValueId(1);
}

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

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