英文:
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 all
,ant 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, "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
答案1
得分: 2
如果您想使用items.xml中的ProductData,您应该使用ProductDataModel而不是ProductData。
英文:
If you want to use ProductData from items.xml you should be using ProductDataModel instead of ProductData.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论