java - JUnit 找不到 ApplicationContext

标签 java spring testing junit

所以在这里使用教程 https://hop2croft.wordpress.com/2011/07/06/jpa-basic-example-with-entitymanager-spring-and-maven/当我作为 JUnit 测试运行时,我不断收到错误消息,提示找不到应用程序上下文。测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/testApplicationContext.xml")
public class CarTest {

是这个,我尝试将 testapplicationcontext.xml 放入目前为止的每个文件夹中。曾经是

 @ContextConfiguration(locations={"/testApplicationContext.xml"}) but I thought if I change it, it might fix it.

有什么建议吗?

它目前位于我必须创建的新文件夹中,srcs/test/resources。我使用 STS 在 Eclipse 中从头开始创建了这个项目。

编辑:在链的末尾,我在我得到的控制台中跟随

Caused by: java.lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence

还有我的pom.xml

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hopcroft.examples.spring</groupId>
<artifactId>simpledao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<pluginRepositories>
    <pluginRepository>
        <id>maven-annotation</id>
        <url>http://maven-annotation-plugin.googlecode.com/svn/trunk/mavenrepo</url>
    </pluginRepository>

</pluginRepositories>



  <repositories>
  <repository>
     <id>JBoss Deprecated</id>
      <url>https://repository.jboss.org/nexus/content/repositories/deprecated</url>
</repository>
<repository>
<id>java.net</id>
  <url>http://download.java.net/maven/2/</url>
  </repository>
 </repositories>
  <build>
        <pluginManagement>
    <plugins>




        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArguments>
                    <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                </compilerArguments>
            </configuration>
            <version>2.3.2</version>
        </plugin>
        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>2.0.2</version>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <outputDirectory>src/main/generated-java</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/generated-java</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
                 </pluginManagement>

</build>

<dependencies>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>ejb3-persistence</artifactId>
        <version>1.0.2.GA</version>
        <type>pom</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.0.5.RELEASE</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.0.0.RELEASE</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.0.5.RELEASE</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>javax.security</groupId>
        <artifactId>jacc</artifactId>
        <version>1.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.10</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.2.2</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.0.5.RELEASE</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.1</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.1.GA</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.5.0-Final</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.0-Final</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.openxma</groupId>
        <artifactId>dsl-platform</artifactId>
        <version>3.6.3</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <version>1.0.0.Final</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
</dependencies>

最佳答案

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("类路径:/testApplicationContext.xml") 公共(public)课 CarTest {

...

关于java - JUnit 找不到 ApplicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40838632/

相关文章:

java - 限制 Runtime.getRuntime().exec 的 CPU 使用率

java - 传入 JMS 消息的事务同步未激活

java - CustomEditorConfigurer 的 customEditors 属性与 RangeArrayPropertyEditor、Spring 4.3.3 与 Spring Batch 3.0.7

c# - 以编程方式测试 asp.net 页面

java - 为什么我不能在包外使用 protected 构造函数?

java - 在 Eclipse 中以 root 身份运行 Java 程序

java - POST 时传递到持久化的分离实体

testing - Jmeter:我在录制脚本时遇到错误

iphone - 窃取应用程序并使用临时分发将其发布到 AppStore

java - android 无法设置按钮的宽度?