我正在尝试让Broadleaf Commerce在简单的JUnit单元测试中运行,以作为概念验证来查看我是否可以执行基本的购物车操作,例如添加到购物车,更新数量和下订单-无需UI。

看看是否可以将Broadleaf用作后端使用完全不同的UI。

到目前为止,开始工作有点困难(只是为了完全启动并运行spring上下文)。我正在使用Gradle构建我的项目。配置如下所示:

测试类别:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
    "MyTest.xml",
})
public class BltMainTest {

@Test
public void test() {
    System.out.println("THIS IS A TEST");
}

}


MyTest.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       ">

<import resource="classpath:bl-framework-applicationContext-persistence.xml"/>
<import resource="classpath:bl-framework-applicationContext-entity.xml"/>
<import resource="classpath:bl-framework-applicationContext-workflow.xml"/>
<import resource="classpath:bl-framework-applicationContext.xml"/>
<import resource="classpath:bl-profile-applicationContext.xml"/>
<import resource="classpath:bl-profile-applicationContext-entity.xml"/>
<import resource="classpath:bl-profile-applicationContext-persistence.xml"/>

</beans>


build.gradle摘录(要使ehcache与Maven Central兼容,必须做一些改动,但不完全清楚为什么):

...
dependencies {
...
 compile 'net.sf.ehcache:ehcache-core:2.6.0'

 compile('org.broadleafcommerce:broadleaf-framework:1.6.1-GA') {
     exclude module: 'ehcache-terracotta'
     exclude module: 'ehcache'
 }
 compile('org.broadleafcommerce:broadleaf-profile:1.6.1-GA') {
     exclude module: 'ehcache-terracotta'
     exclude module: 'ehcache'
 }
...
}


我得到的堆栈跟踪相当长,但是最底层(我认为相关)是:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blEntityManagerFactorySecureInfo' defined in class path resource [bl-framework-applicationContext-persistence.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:99)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findNamedEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:511)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:493)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:657)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:630)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:339)
    ... 55 more
Caused by: java.lang.NullPointerException
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:527)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:268)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 69 more


从类上看,似乎有一个未指定的持久性单元-但我不确定在哪里可以纠正它。

最重要的是-当我更改config.xml文件中的Spring导入行的顺序时,错误发生了变化。

也许我正在以错误的方式处理此问题,但是我假设我应该能够通过按正确的顺序包含默认的Broadleaf Spring XML来使此工作正常进行-但我尝试了很多,但没有尝试(相同错误,有些不同,都不好)。

关于如何使它起作用的任何想法?

最佳答案

在深入研究了这一点之后,我的确做了这项工作。需要一些特定的东西:


原始帖子中的堆栈跟踪是由blPersistenceUnitManager bean在两个不同的Spring上下文文件中定义的(不确定如何在常规的Broadleaf设置中将它们合并)引起的,其中一个覆盖了另一个。解决方案是在导入所有结合了配置的内容后定义一个,以便从所需的每个持久性单元读取数据。
我必须显式使用3.6.8.Final Hibernate库而不是最新的4.x(关于无法弄清楚Map类型列的处理方式,这有些奇怪的错误)
必须定义blPasswordEncoder Bean
注意:要解决EHCache问题,需要以下示例中的blCacheManager bean:http://jira.broadleafcommerce.org/browse/BLC-599


这是Spring上下文的最终功用:

<import resource="classpath:bl-common-applicationContext.xml"/>

<import resource="classpath:bl-framework-applicationContext.xml"/>
<import resource="classpath:bl-profile-applicationContext.xml"/>

<import resource="classpath:bl-framework-applicationContext-workflow.xml"/>

<import resource="classpath:bl-framework-applicationContext-persistence.xml"/>
<import resource="classpath:bl-profile-applicationContext-persistence.xml"/>

<import resource="classpath:bl-framework-applicationContext-entity.xml"/>
<import resource="classpath:bl-profile-applicationContext-entity.xml"/>

<bean id="blCacheManager" class="org.broadleafcommerce.common.extensibility.cache.ehcache.MergeEhCacheManagerFactoryBean">
    <property name="shared" value="true"/>
</bean>

<bean id="blPersistenceUnitManager" class="org.broadleafcommerce.common.extensibility.jpa.MergePersistenceUnitManager">
    <property name="persistenceXmlLocations">
        <list>
            <value>classpath*:/META-INF/persistence-common.xml</value>
            <value>classpath*:/META-INF/persistence-framework.xml</value>
            <value>classpath*:/META-INF/persistence-profile.xml</value>
        </list>
    </property>
</bean>

<bean id="blPasswordEncoder" class="org.springframework.security.authentication.encoding.PlaintextPasswordEncoder"/>


这使我到达了构建单元测试,运行,Hibernate环境启动且我可以@Resource()等的地步。不确定所有功能的功能如何,但是我认为我将发布答案问题,因为我走了那么远。

07-24 13:50