1

我试图让 Broadleaf Commerce 在一个简单的 JUnit 单元测试中运行,作为概念证明,看看我是否可以在没有 UI 的情况下执行基本的购物车操作,例如添加到购物车、更新数量和下订单。

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

到目前为止,这有点难以开始工作(只是为了让 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 摘录(不得不做一些 hack 来让 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 XMLs 来使其工作 - 但我已经尝试了其中的一些并且没有骰子(有些相同错误,有些不同,都不好)。

关于如何让它发挥作用的任何想法?

4

1 回答 1

0

在深入研究了这个之后,我确实得到了这个工作。需要一些具体的东西:

  1. 原始帖子中的堆栈跟踪是由在两个不同的 Spring 上下文文件中定义的 blPersistenceUnitManager bean 引起的(不确定这些文件是如何在正常的 Broadleaf 设置中合并的),一个覆盖了另一个。解决方案是在导入所有组合配置的内容后定义一个,以便从所需的每个持久性单元读取。
  2. 我必须明确使用 3.6.8.Final Hibernate 库而不是最新的 4.x(一些奇怪的错误,关于无法弄清楚如何处理 Map 类型的列)
  3. 必须定义 blPasswordEncoder bean
  4. 注意:下面示例中的 blCacheManager bean 是解决 EHCache 问题所必需的: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() 东西等的地步。不确定它的功能如何,但我想我会发布这个答案问题,因为我走了那么远。

于 2012-09-17T01:33:25.700 回答