Hybris. 由于 NPE 异常 Item.getImplementation() 未找到,无法运行任何测试。

huangapple go评论99阅读模式
英文:

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

问题

我在本地运行Hybris。但在运行测试时出现错误:

java.lang.NullPointerException: 无法调用"de.hybris.platform.jalo.Item$ItemImpl.isLocalCachingSupported()",因为"de.hybris.platform.jalo.Item.getImplementation()"的返回值为null

ProductData的setter代码行中:

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

集成测试和单元测试都返回错误。

在此之前,使用ant yunitinit有帮助。但这次我甚至尝试了ant clean allant initialize,但没有任何改变。

这是我添加的项目类型。它生成了正确的表和类。

<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>

以及测试类本身:

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);
    }
}

测试是通过的,但我更改了ProductData的声明。我使用了items.xml,而不仅仅是在包中声明类。我想也许这里有孤立的部分,但我没有找到。

错误的原因是我已经在数据库中有ProductData,并且我从items.xml中删除了ProductData的声明,如答案建议的,而是声明了类。

英文:

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, &quot;code&quot;.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.

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

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(&quot;productCode&quot;);
        when(productModel.getWeight()).thenReturn(100.0);

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

        assertEquals(&quot;productCode&quot;, 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

答案1

得分: 2

如果您想使用items.xml中的ProductData,您应该使用ProductDataModel而不是ProductData。

英文:

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

huangapple
  • 本文由 发表于 2023年2月7日 02:28:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定