0%

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

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

1
2
3
4
5
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代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
*
*/
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框架通常需要的一些环境变量。 在测试用例中加入如下代码可以解决问题。

1
2
3
4
5
6
7
@Before
public void setUp() throws Exception
{
createCoreData();
createDefaultCatalog();
createHardwareCatalog();
}