2016-09-17 23 views
1

我是hybris電子商務的初學者。我需要與同步產品目錄進行集成測試,但同步時出現問題。我添加了新的產品屬性 - 「classification」,並在測試中爲此屬性設置了值。然後performCronJob方法做syncJobStaged產品目錄,到Online產品目錄。現在,當從Online目錄獲得產品時,它不具有「classification」屬性的值,並且出現AssertionError。請告訴我,爲什麼會發生這種情況。這是我的考驗。Hybris電子商務 - 同步集成測試

@RunWith(HybrisJUnit4ClassRunner.class) 
@RunListeners(
{ TransactionRunListener.class, ItemCreationListener.class, LogRunListener.class, PlatformRunListener.class }) 
@Transactional 
public class ProductMyIntegrationTest extends ServicelayerTransactionalTest 
{ 
    @Resource 
    private TypeService typeService; 
    @Resource 
    private ModelService modelService; 
    @Resource 
    private CatalogVersionService catalogVersionService; 
    @Resource 
    private ProductService productService; 
    @Resource 
    private CronJobService cronJobService; 

    public ProductService getProductService() 
    { 
     return productService; 
    } 

    public CatalogVersionService getCatalogVersionService() 
    { 
     return catalogVersionService; 
    } 

    public TypeService getTypeService() 
    { 
     return typeService; 
    } 

    public ModelService getModelService() 
    { 
     return modelService; 
    } 

    @Test 
    public void testProductSyncBehavior() 
    { 
     final CatalogVersionModel catalogStagedVersionModel = getCatalogVersionService().getCatalogVersion("hybrisProductCatalog", 
       "Staged"); 
     final CatalogVersionModel catalogOnlineVersionModel = getCatalogVersionService().getCatalogVersion("hybrisProductCatalog", 
       "Online"); 
     final Collection<CatalogVersionModel> coll = new ArrayList<>(); 
     coll.add(catalogOnlineVersionModel); 
     coll.add(catalogStagedVersionModel); 
     catalogVersionService.setSessionCatalogVersions(coll); 

     final ProductModel product = productService.getProduct(catalogStagedVersionModel, "0100"); 
     product.setClassification("RRRRRRR"); 
     getModelService().save(product); 

     cronJobService.performCronJob((CatalogVersionSyncCronJobModel) modelService.get(PK.fromLong(8796453503477L)), true); 

     final ProductModel prodOnline = modelService.get(productService.getProduct(catalogOnlineVersionModel, "0100").getPk()); 
     final ProductModel prodStaged = modelService.get(productService.getProduct(catalogStagedVersionModel, "0100").getPk()); 
     Assert.assertNotNull(prodOnline.getClassification()); 
    } 
} 

Asse田:

java.lang.AssertionError 
    at org.junit.Assert.fail(Assert.java:86) 
    at org.junit.Assert.assertTrue(Assert.java:41) 
    at org.junit.Assert.assertNotNull(Assert.java:712) 
    at org.junit.Assert.assertNotNull(Assert.java:722) 
    at de.hybris.merchandise.core.product.classification.ProductMyIntegrationTest.testProductSyncBehavior(ProductMyIntegrationTest.java:91) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source)... 

非常感謝你。

回答

0

我決定了我的問題。我只是擴展ServicelayerTest而沒有事務和同步成功。但我不明白爲什麼Transactional不起作用。

0

使用@Transactional意味着你的測試的初始化過程中一開始事務,你的測試後(執行@After後)回滾(@Before被執行前),因此所有的數據,你居然保存使用服務層永遠不會提交到數據庫,這就是爲什麼您無法看到新屬性的更改。

這似乎是不可預知的,因爲您無法確定同步cronjob在測試屬性之前是否有足夠的時間同步產品。