Leo Wang's Notepad

在SAP hybris中实行单体测试(Unit Test)的注意事项

在SAP hybris的单体测试中,出现如下错误信息:

ERROR [main] (junit) [FlexibleSearch] Flexiblesearch error: could not translate value expression 'session.catalogversions'
ERROR [main] (junit) [FlexibleSearch] query was 'SELECT {cat.pk} FROM {Category AS cat} WHERE NOT EXISTS ({{SELECT {pk} FROM {CategoryCategoryRelation AS rel JOIN Category AS super ON {rel.source}={super.PK} } WHERE {rel:target}={cat.pk} AND {super.catalogVersion}={cat.catalogVersion} }}) AND {cat.catalogVersion} = ?catalogVersion'
ERROR [main] (junit) [FlexibleSearch] translated query was: SELECT  item_t0.PK  FROM junit_categories item_t0 WHERE (NOT EXISTS (SELECT  item_t1.PK  FROM junit_cat2catrel item_t1 JOIN junit_categories item_t2 ON  item_t1.SourcePK = item_t2.PK  WHERE ( item_t1.TargetPK = item_t0.PK  AND  item_t2.p_catalogversion = item_t0.p_catalogversion ) AND ((item_t1.TypePkString=?  AND item_t2.TypePkString IN  (?,?,?,?)  AND (( item_t2.p_catalogversion  IN (?))) ))) AND  item_t0.p_catalogversion  = ?) AND (item_t0.TypePkString IN  (?,?,?,?)  AND (( item_t0.p_catalogversion  IN (?))) )
ERROR [main] (junit) [LogRunListener] Test method testCategoryNav(org.training.renderer.CategoryNavComponentRendererTest) failed!!
de.hybris.platform.servicelayer.search.exceptions.FlexibleSearchException: could not translate value expression 'session.catalogversions'

Java代码如下:

/**
 *
 */
package org.training.renderer;

import de.hybris.platform.catalog.CatalogVersionService;
import de.hybris.platform.catalog.model.CatalogVersionModel;
import de.hybris.platform.category.CategoryService;
import de.hybris.platform.category.model.CategoryModel;
import de.hybris.platform.servicelayer.ServicelayerTransactionalTest;

import java.util.Collection;

import javax.annotation.Resource;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;


/**
 * @author lwang
 *
 */
public class CategoryNavComponentRendererTest extends ServicelayerTransactionalTest
{

	@Resource(name = "categoryService")
	private CategoryService categoryService;

	@Resource(name = "catalogVersionService")
	private CatalogVersionService catalogVersionService;

	@Test
	public void testCategoryNav()
	{
		final CatalogVersionModel catalogVersion = catalogVersionService.getCatalogVersion("uaProductCatalog", "Online");
		Assert.assertNotNull(catalogVersion);
		final Collection<CategoryModel> categoryList = categoryService.getRootCategoriesForCatalogVersion(catalogVersion);
		Assert.assertNotNull(categoryList);
	}
}

之所以会出现错误,是因为程序没有加载hybris框架通常需要的一些环境变量。

在测试用例中加入如下代码可以解决问题。

	@Before
	public void setUp() throws Exception
	{
		createCoreData();
		createDefaultCatalog();
		createHardwareCatalog();
	}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注