开启注解 beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/sc
开启注解
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd" > <context:component-scan base-package="concerttours"/>
DAO
package concerttours.daos; import java.util.List; import concerttours.model.BandModel; /** * An interface for the Band DAO including various operations for retrieving persisted Band model objects */ public interface BandDAO { List<BandModel> findBands(); List<BandModel> findBandsByCode(String code); }
DAOImpl
package concerttours.daos.impl; import de.hybris.platform.servicelayer.search.FlexibleSearchQuery; import de.hybris.platform.servicelayer.search.FlexibleSearchService; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import concerttours.daos.BandDAO; import concerttours.model.BandModel; @Component(value = "bandDAO") public class DefaultBandDAO implements BandDAO { @Autowired private FlexibleSearchService flexibleSearchService; @Override public List<BandModel> findBands() { // 查询 final String queryString = // "SELECT {p:" + BandModel.PK + "} "// + "FROM {" + BandModel._TYPECODE + " AS p} "; final FlexibleSearchQuery query = new FlexibleSearchQuery(queryString); return flexibleSearchService.<BandModel> search(query).getResult(); } @Override public List<BandModel> findBandsByCode(final String code) { // 带参查询 final String queryString = // "SELECT {p:" + BandModel.PK + "}" // + "FROM {" + BandModel._TYPECODE + " AS p} "// + "WHERE " + "{p:" + BandModel.CODE + "}=?code "; final FlexibleSearchQuery query = new FlexibleSearchQuery(queryString); query.addQueryParameter("code", code); return flexibleSearchService.<BandModel> search(query).getResult(); } }
这里用了@Component(value = "bandDAO")来实现<bean id="" class=""/>
service
package concerttours.service; import java.util.List; import concerttours.model.BandModel; public interface BandService { List<BandModel> getBands(); BandModel getBandForCode(String code); }
serviceImpl
package concerttours.service.impl; import de.hybris.platform.servicelayer.exceptions.AmbiguousIdentifierException; import de.hybris.platform.servicelayer.exceptions.UnknownIdentifierException; import java.util.List; import org.springframework.beans.factory.annotation.Required; import concerttours.daos.BandDAO; import concerttours.model.BandModel; import concerttours.service.BandService; public class DefaultBandService implements BandService { @Resource private ModelService modelService; private BandDAO bandDAO; @Override public List<BandModel> getBands() { return bandDAO.findBands(); } @Test public void Test() { //插入 final XXX xxx = modelService.create(XXX.class); xxx.sexxx(aaa); modelService.save(xxx); //批量1 UserModel user = userService.getUserForUID(testUid); final UserGroupModel group = modelService.create(UserGroupModel.class); group.setUid("testGroup2"); group.setMaxBruteForceLoginAttempts(Integer.valueOf(3)); user.setGroups((Set) ImmutableSet.builder().addAll(user.getGroups()).add(group).build()); modelService.saveAll(user, group); } @Override public BandModel getBandForCode(final String code) throws AmbiguousIdentifierException, UnknownIdentifierException { final List<BandModel> result = bandDAO.findBandsByCode(code); if (result.isEmpty()) { throw new UnknownIdentifierException("Band with code '" + code + "' not found!"); } else if (result.size() > 1) { throw new AmbiguousIdentifierException("Band code '" + code + "' is not unique, " + result.size() + " bands found!"); } return result.get(0); } @Required public void setBandDAO(final BandDAO bandDAO) { this.bandDAO = bandDAO; } }
这里在xml中配置了Service,service中注入了dao,所以dao可以直接注入,get、set,不用 @Autowired
<alias name = "defaultBandService" alias = "bandService" /> <bean id = "defaultBandService" class = "concerttours.service.impl.DefaultBandService" > <property name = "bandDAO" ref = "bandDAO" /> </bean>
集成测试
package concerttours.daos.impl; import static org.junit.Assert.assertTrue; import de.hybris.bootstrap.annotations.IntegrationTest; import de.hybris.platform.servicelayer.ServicelayerTransactionalTest; import de.hybris.platform.servicelayer.model.ModelService; import java.util.List; import javax.annotation.Resource; import org.junit.Assert; import org.junit.Test; import concerttours.daos.BandDAO; import concerttours.model.BandModel; @IntegrationTest public class DefaultBandDAOIntegrationTest extends ServicelayerTransactionalTest { @Resource private BandDAO bandDAO; @Resource private ModelService modelService; private static final String BAND_CODE = "ROCK-11"; private static final String BAND_NAME = "Ladies of Rock"; private static final String BAND_HISTORY = "All female rock band formed in Munich in the late 1990s"; private static final Long ALBUMS_SOLD = Long.valueOf(1000L); @Test public void bandDAOTest() { List<BandModel> bandsByCode = bandDAO.findBandsByCode(BAND_CODE); assertTrue("No Band should be returned", bandsByCode.isEmpty()); List<BandModel> allBands = bandDAO.findBands(); final int size = allBands.size(); final BandModel bandModel = modelService.create(BandModel.class); bandModel.setCode(BAND_CODE); bandModel.setName(BAND_NAME); bandModel.setHistory(BAND_HISTORY); bandModel.setAlbumSales(ALBUMS_SOLD); modelService.save(bandModel); // check we now get one more band back than previously and our test band is in the list allBands = bandDAO.findBands(); Assert.assertEquals(size + 1, allBands.size()); Assert.assertTrue("band not found", allBands.contains(bandModel)); // check we can locate our test band by its code bandsByCode = bandDAO.findBandsByCode(BAND_CODE); Assert.assertEquals("Did not find the Band we just saved", 1, bandsByCode.size()); Assert.assertEquals("Retrieved Band's code attribute incorrect", BAND_CODE, bandsByCode.get(0).getCode()); Assert.assertEquals("Retrieved Band's name attribute incorrect", BAND_NAME, bandsByCode.get(0).getName()); Assert.assertEquals("Retrieved Band's albumSales attribute incorrect", ALBUMS_SOLD, bandsByCode.get(0).getAlbumSales()); Assert.assertEquals("Retrieved Band's history attribute incorrect", BAND_HISTORY, bandsByCode.get(0).getHistory()); } @Test public void testFindBands_EmptyStringParam() { //calling findBandsByCode() with an empty String - returns no results final List<BandModel> bands = bandDAO.findBandsByCode(""); Assert.assertTrue("No Band should be returned", bands.isEmpty()); } @Test(expected = IllegalArgumentException.class) public void testfindBands_NullParam() { //calling findBandByCode with null should throw an IllegalArgumentException bandDAO.findBandsByCode(null); //method's return value not captured } }
ant
ant all ant yunitinit ant integrationtests -Dtestclasses.packages = concerttours。* 在<HYBRIS_HOME_DIR> /hybris/log/junit/index.html查看测试结果
单元测试
package concerttours.service.impl; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import de.hybris.bootstrap.annotations.UnitTest; import java.util.Arrays; import java.util.Collections; import java.util.List; import org.junit.Before; import org.junit.Test; import concerttours.daos.BandDAO; import concerttours.model.BandModel; /** * This test file tests and demonstrates the behavior of the BandService's methods getAllBand, getBand and saveBand. * * We already have a separate file for testing the Band DAO, and we do not want this test to implicitly test that in * addition to the BandService. This test therefore mocks out the Band DAO leaving us to test the Service in isolation, * whose behavior should be simply to wraps calls to the DAO: forward calls to it, and passing on the results it * receives from it. */ @UnitTest public class DefaultBandServiceUnitTest { private DefaultBandService bandService; private BandDAO bandDAO; private BandModel bandModel; /** Name of test band. */ private static final String BAND_CODE = "Ch00X"; /** Name of test band. */ private static final String BAND_NAME = "Singers All"; /** History of test band. */ private static final String BAND_HISTORY = "Medieval choir formed in 2001, based in Munich famous for authentic monastic chants"; @Before public void setUp() { // We will be testing BandServiceImpl - an implementation of BandService bandService = new DefaultBandService(); // So as not to implicitly also test the DAO, we will mock out the DAO using Mockito bandDAO = mock(BandDAO.class); // and inject this mocked DAO into the BandService bandService.setBandDAO(bandDAO); // This instance of a BandModel will be used by the tests bandModel = new BandModel(); bandModel.setCode(BAND_CODE); bandModel.setName(BAND_NAME); bandModel.setAlbumSales(null); bandModel.setHistory(BAND_HISTORY); } /** * This test tests and demonstrates that the Service's getAllBands method calls the DAOs' getBands method and returns * the data it receives from it. */ @Test public void testGetAllBands() { // We construct the data we would like the mocked out DAO to return when called final List<BandModel> bandModels = Arrays.asList(bandModel); //Use Mockito and compare results when(bandDAO.findBands()).thenReturn(bandModels); // Now we make the call to BandService's getBands() which we expect to call the DAOs' findBands() method final List<BandModel> result = bandService.getBands(); // We then verify that the results returned from the service match those returned by the mocked-out DAO assertEquals("We should find one", 1, result.size()); assertEquals("And should equals what the mock returned", bandModel, result.get(0)); } @Test public void testGetBand() { // Tell Mockito we expect a call to the DAO's getBand(), and, if it occurs, Mockito should return BandModel instance when(bandDAO.findBandsByCode(BAND_CODE)).thenReturn(Collections.singletonList(bandModel)); // We make the call to the Service's getBandForCode() which we expect to call the DAO's findBandsByCode() final BandModel result = bandService.getBandForCode(BAND_CODE); // We then verify that the result returned from the Service is the same as that returned from the DAO assertEquals("Band should equals() what the mock returned", bandModel, result); } }
ant
ant all ant unittests -Dtestclasses.packages = concerttours。*
Facade
创建DTO
<bean class = "concerttours.data.TourSummaryData"> <description >Data object for a tour summary which has no equivalent on the type system</description> <property name = "id" type = "String" /> <property name = "tourName" type = "String" /> <property name = "numberOfConcerts" type = "String" /> </bean>
这里通过xml逆向生成DTO实体类
定义Facade接口
package concerttours.facades; import concerttours.data.TourData; public interface TourFacade { BandData getBand(String name); }
定义Facade实现类
public class DefaultBandFacade implements BandFacade { private BandService bandService; @Override public List<BandData> getBands() { final List<BandModel> bandModels = bandService.getBands(); final List<BandData> bandFacadeData = new ArrayList<>(); for (final BandModel sm : bandModels) { final BandData sfd = new BandData(); sfd.setId(sm.getCode()); sfd.setName(sm.getName()); sfd.setDescription(sm.getHistory()); sfd.setAlbumsSold(sm.getAlbumSales()); bandFacadeData.add(sfd); } return bandFacadeData; } }
注入
<alias name = "defaultBandFacade" alias = "bandFacade" /> <bean id = "defaultBandFacade" class ="concerttours.facades.impl.DefaultBandFacade" > <property name = "bandService" ref = "bandService" /> </bean>