0

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());
    }
}
dobby
  • 23
  • 6
  • Where are the setters called in the unit tests? Side bar: consider using [lombok](https://projectlombok.org/) which would remove the need to test. – Andrew S Oct 16 '20 at 18:03
  • I am using the wrong entity class. It should be ProductAttributeEntityPK class instead of ProductAttributeEntity class – dobby Oct 16 '20 at 18:28

1 Answers1

0

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);
}
Mustafa CEVIK
  • 23
  • 1
  • 10