Hybris (4) Testing the DAO and the Service

Submitted by Lizhe on Thu, 01/31/2019 - 08:32

 

要测试Dao, 你首先得创建Dao

20190131043342

在console的tab 页里选择 

20190131043625

 

20190131043817

点击 junit tenant 

确认我们自己定义的extension在列表中

20190131043954

点击 initialize tenant 按钮

20190131044733

 

初始化完成之后, 数据库里多出一堆表

可以看到我们自定义的表

20190131045239

还可以看到 带 junit_ 前缀的一样的表

20190131045308

创建测试文件

注意这里因为使用的是 带 junit_ 前缀的表, 现在还是一个空表, 所以我们的断言里使用的 list size 是 0

package name.lizhe.dao.impl;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
 
import de.hybris.platform.servicelayer.ServicelayerTransactionalTest;
import de.hybris.platform.servicelayer.model.ModelService;
 
import java.util.List;
 
import javax.annotation.Resource;
 
import org.junit.Test;

import name.lizhe.dao.LizhebuglistDao;
 

public class LizhebuglistDaoImplTest extends ServicelayerTransactionalTest
{
    @Resource
    private LizhebuglistDao lizhebuglistDao;
 
    @Resource
    private ModelService modelService;
 
    @Test
    public void lizhebuglistDaoTest()
    {
        assertEquals(0, lizhebuglistDao.getBugList().size());
    }
 
}

20190131061629

 

测试 service 层实际上跟上面的dao测试几乎是一样的

/lizhebuglist/testsrc/name/lizhe/service/impl/DefaultLizhebuglistServiceIntegrationTest.java

/*
 * [y] hybris Platform
 *
 * Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved.
 *
 * This software is the confidential and proprietary information of SAP
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the
 * license agreement you entered into with SAP.
 */
package name.lizhe.service.impl;

import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static name.lizhe.constants.LizhebuglistConstants.PLATFORM_LOGO_CODE;

import de.hybris.bootstrap.annotations.IntegrationTest;
import de.hybris.platform.core.model.media.MediaModel;
import de.hybris.platform.servicelayer.ServicelayerBaseTest;
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;

import javax.annotation.Resource;

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

import name.lizhe.dao.LizhebuglistDao;
import name.lizhe.service.LizhebuglistService;
import name.lizhe.service.impl.DefaultLizhebuglistService;


/**
 * This is an example of how the integration test should look like. {@link ServicelayerBaseTest} bootstraps platform so
 * you have an access to all Spring beans as well as database connection. It also ensures proper cleaning out of items
 * created during the test after it finishes. You can inject any Spring service using {@link Resource} annotation. Keep
 * in mind that by default it assumes that annotated field name matches the Spring Bean ID.
 */
@IntegrationTest
public class DefaultLizhebuglistServiceIntegrationTest extends ServicelayerBaseTest
{
    @Resource
    private LizhebuglistService lizhebuglistService;
    @Resource
    private FlexibleSearchService flexibleSearchService;

    //@Before
    public void setUp() throws Exception
    {
        lizhebuglistService.createLogo(PLATFORM_LOGO_CODE);
    }

    //@Test
    public void shouldReturnProperUrlForLogo() throws Exception
    {
        // given
        final String logoCode = "lizhebuglistPlatformLogo";

        // when
        final String logoUrl = lizhebuglistService.getHybrisLogoUrl(logoCode);

        // then
        assertThat(logoUrl).isNotNull();
        assertThat(logoUrl).isEqualTo(findLogoMedia(logoCode).getURL());
    }

    private MediaModel findLogoMedia(final String logoCode)
    {
        final FlexibleSearchQuery fQuery = new FlexibleSearchQuery("SELECT {PK} FROM {Media} WHERE {code}=?code");
        fQuery.addQueryParameter("code", logoCode);

        return flexibleSearchService.searchUnique(fQuery);
    }
    
    @Test
    public void test() {
        assertEquals(0, lizhebuglistService.getBugList().size());
    }

}
 

20190131063304