我试图让 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 来使其工作 - 但我已经尝试了其中的一些并且没有骰子(有些相同错误,有些不同,都不好)。
关于如何让它发挥作用的任何想法?