javamockitosap-commerce-cloudhybris-data-hub

Hybris. Couldn't run any test because of NPE exception Item.getImplementation() not found


I run Hybris locally. But I'm getting error while running tests:

java.lang.NullPointerException: Cannot invoke "de.hybris.platform.jalo.Item$ItemImpl.isLocalCachingSupported()" because the return value of "de.hybris.platform.jalo.Item.getImplementation()" is null

at ProductData setter code lines:

    public void setCode(final SessionContext ctx, final String value)
{
    setProperty(ctx, "code".intern(),value);
}

Integration and Unit test are same returns errors.

Before that ant yunitinit helped me. But this time I even have tried ant clean all, ant initialize but no changings.

It's my added item type. It generated correct table and class.

        <itemtype code="ProductData" autocreate="true" generate="true">
        <deployment table="ProductData" typecode="30800"/>
        <attributes>
            <attribute qualifier="code" type="java.lang.String">
                <persistence type="property"/>
                <modifiers read="true" write="true" search="true"/>
            </attribute>
            <attribute qualifier="weight" type="java.lang.Double">
                <persistence type="property"/>
                <modifiers read="true" write="true" search="true" optional="true"/>
            </attribute>
            <attribute qualifier="height" type="java.lang.Double">
                <persistence type="property"/>
                <modifiers read="true" write="true" search="true" optional="true"/>
            </attribute>
            <attribute qualifier="length" type="java.lang.Double">
                <persistence type="property"/>
                <modifiers read="true" write="true" search="true" optional="true"/>
            </attribute>
            <attribute qualifier="width" type="java.lang.Double">
                <persistence type="property"/>
                <modifiers read="true" write="true" search="true" optional="true"/>
            </attribute>
        </attributes>
    </itemtype>

And test class itself:

package com.epam.trainingextension.setup.populator;

import com.epam.trainingextension.enums.ProductOption;
import com.epam.trainingextension.jalo.ProductData;
import de.hybris.bootstrap.annotations.IntegrationTest;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.Registry;
import de.hybris.platform.core.model.product.ProductModel;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ProductPopulatorTest {
    @InjectMocks
    private ProductPopulator productPopulator;

    @Mock
    private ProductModel productModel;

    @Test
    public void testPopulate_whenPhysicalDimensionsOptionIsPassed_shouldPopulateWeight()
    {
        when(productModel.getCode()).thenReturn("productCode");
        when(productModel.getWeight()).thenReturn(100.0);

        final ProductData productData = new ProductData();
        productPopulator.populate(productModel, productData, Collections.singletonList(ProductOption.PHYSICAL_DIMENSIONS));

        assertEquals("productCode", productData.getCode());
        assertEquals(100.0, productData.getWeight(), 0.0);
    }
}

Test was green, but I've changed declaration of ProductData. I used items.xml instead of just usual declaration of class in packages. I thought maybe here was left orphans, but I don't have one.

Cause of error was that I had already ProductData in db, and I removed declaration of ProductData from items.xml how suggested in answers, and declared class instead


Solution

  • If you want to use ProductData from items.xml you should be using ProductDataModel instead of ProductData.