简体   繁体   中英

How to Unit test JPA idclass

I am struggling to write/understand a unit test for JPA inheritance @idClass in Intellij IDE. When running the test with code coverage, the IDE displays 5/6 method covered. But the class has only five methods. Where is the 6th method? 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 . You can't get a 100% covarage value because you do not provide all the controls in the equals method. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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