我的应用程序只是测试环境是否基本正常工作。因此,我设置了一个对应于“Hello Worlds”示例的测试类,该示例可以在 SpringSource 的 gitHub 存储库中找到。Neo4j 作为静态 Web 应用程序包含在内并嵌入运行。不一定需要 Web 界面。问题是,当我开始通过 Junit 运行测试用例时,我收到如下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'GalaxyServiceTests': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private GalaxyService GalaxyServiceTests.galaxyService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [GalaxyService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我的应用程序配置是这样的
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">
<context:annotation-config/>
<bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
destroy-method="shutdown">
<constructor-arg index="0" value="target/graph.db" />
<constructor-arg index="1">
<map><entry key="enable_remote_shell" value="true"/></map>
</constructor-arg>
</bean>
<!-- <bean id="serverWrapper" class="org.neo4j.server.WrappingNeoServerBootstrapper" -->
<!-- init-method="start" destroy-method="stop"> -->
<!-- <constructor-arg ref="graphDatabaseService"/> -->
<!-- </bean> -->
</beans>
测试类非常简单
@ContextConfiguration( "file:c:/Users/ben/Dropbox/Semester 6/VM Graphentheorie/devEnv/bodega/application-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class GalaxyServiceTests {
// @Autowired
// private GalaxyService galaxyService;
@Autowired
private Neo4jTemplate template;
@Rollback(false)
@BeforeTransaction
public void cleanUpGraph() {
Neo4jHelper.cleanDb(template);
}
@Test
public void allowWorldCreation(){
// GraphDatabaseService graphDatabaseService = new GraphDatabaseFactory().newEmbeddedDatabase( "target/graph.db" );
// Node abc = graphDatabaseService.createNode();
// abc.setProperty("name","ben");
World abc = new World("Erde",1);
template.save(abc);
我的 pom 看起来像这样:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.0.7.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>2.0.0-M02</version>
<classifier>static-web</classifier>
</dependency>
由于我刚接触到与 neo4j 相关的 spring,因此我无法解决此错误。如果有人得到提示或建议 - 在此先感谢
编辑 我将 spring-data 的依赖项从 2.2.0.BUILD-SNAPSHOT 更改为 2.2.0.RELEASE .. 现在 neo4j 正在运行,直到将错误传递给控制台:
21:11:48.196 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@7e26f5a7] to prepare test instance [GalaxyServiceTests@64d74895]
java.lang.IllegalStateException: Failed to load ApplicationContext
EDIT2 我已经在 http://github.com/BenMatheja/bodega上发布了存储库
EDIT3
application-context.xml 路径的东西搞砸了。我解决了这个问题@ContextConfiguration( locations = {"classpath:/META-INF/application-context.xml"})
我正处于一个例外的地步
没有为依赖找到 [start.GalaxyService] 类型的合格 bean:预计至少有 1 个 bean 有资格作为此依赖的自动装配候选者
现在每次运行 JUnit4 测试时都会抛出。
EDIT4 我昨天设法让它工作。应用程序上下文的某些部分非常混乱。
<context:spring-configured/>
<context:annotation-config/>
<context:component-scan base-package="components"/>
<neo4j:config graphDatabaseService="graphDatabaseService"/>
<neo4j:config storeDirectory="target/graph.db"/>
<neo4j:repositories base-package="Repositories"/>
这是我的工作应用程序上下文的片段。此外,我错过了将 @Service 附加到 GalaxyService 类。
现在一切都按预期工作。这可能会帮助面临同样问题的人。