Hybris (4) Testing the DAO and the Service

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


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


在console的tab 页里选择 




点击 junit tenant 



点击 initialize tenant 按钮



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



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



注意这里因为使用的是 带 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
    private LizhebuglistDao lizhebuglistDao;
    private ModelService modelService;
    public void lizhebuglistDaoTest()
        assertEquals(0, lizhebuglistDao.getBugList().size());



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


 * [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.
public class DefaultLizhebuglistServiceIntegrationTest extends ServicelayerBaseTest
    private LizhebuglistService lizhebuglistService;
    private FlexibleSearchService flexibleSearchService;

    public void setUp() throws Exception

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

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

        // then

    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);
    public void test() {
        assertEquals(0, lizhebuglistService.getBugList().size());