我正在尝试对spring jpa / hibernate应用程序进行junit测试。当我试图从日食运行junit时,我遇到了错误。对于我在内存数据库中使用的junit。任何人都可以帮我找到这个问题的原因。
我的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.common</groupId>
<artifactId>cricketapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>cricketapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.1.4.RELEASE</spring.version>
<hibernate.version>4.1.9.Final</hibernate.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.8</version>
</dependency>
</dependencies>
<build>
<finalName>cricketapp</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
我的mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.cricketapp.controller" />
<context:component-scan base-package="com.cricketapp.service" />
<context:component-scan base-package="com.cricketapp.domain" />
<mvc:annotation-driven />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cricketapp" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.cricketapp.domain" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
我的mvc-dispatcher-servlet-test.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<import resource="classpath:mvc-dispatcher-servlet.xml"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.cricketapp.domain" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:cricketapp" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
</beans>
我的Junit课程
@ContextConfiguration(locations = "classpath:mvc-dispatcher-servlet-test.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class CricketAppServiceTest {
@Autowired
TeamService teamService;
@Test
public void testCreateTeam() {
Team team = new Team();
team.setName("RCB");
teamService.createTeam(team);
List<Team> retrievedTeam = teamService.getTeam();
Assert.assertEquals(team.getName(),retrievedTeam.get(0).getName());
}
}
错误详情
java.lang.IllegalStateException: Could not load TestContextBootstrapper [class org.springframework.test.context.support.DefaultTestContextBootstrapper]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.
at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:87)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:102)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:124)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:115)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;
at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:81)
... 20 more
Mvn依赖树详细信息
INFO] ------------------------------------------------------------------------
INFO] Building cricketapp Maven Webapp 1.0-SNAPSHOT
INFO] ------------------------------------------------------------------------
INFO]
INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ cricketapp ---
INFO] com.mkyong.common:cricketapp:war:1.0-SNAPSHOT
INFO] +- org.springframework:spring-core:jar:4.1.4.RELEASE:compile
INFO] | \- commons-logging:commons-logging:jar:1.2:compile
INFO] +- org.springframework:spring-web:jar:4.1.4.RELEASE:compile
INFO] | +- org.springframework:spring-aop:jar:4.1.4.RELEASE:compile
INFO] | | \- aopalliance:aopalliance:jar:1.0:compile
INFO] | \- org.springframework:spring-beans:jar:4.1.4.RELEASE:compile
INFO] +- org.springframework:spring-webmvc:jar:4.1.4.RELEASE:compile
INFO] | \- org.springframework:spring-expression:jar:4.1.4.RELEASE:compile
INFO] +- org.springframework:spring-context:jar:4.1.4.RELEASE:compile
INFO] +- org.springframework:spring-tx:jar:4.1.4.RELEASE:compile
INFO] +- org.springframework:spring-orm:jar:4.1.4.RELEASE:compile
INFO] | \- org.springframework:spring-jdbc:jar:4.1.4.RELEASE:compile
INFO] +- junit:junit:jar:4.11:test
INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
INFO] +- org.springframework:spring-test:jar:4.1.4.RELEASE:test
INFO] +- org.hibernate:hibernate-entitymanager:jar:4.1.9.Final:compile
INFO] | +- org.jboss.logging:jboss-logging:jar:3.1.0.GA:compile
INFO] | +- org.javassist:javassist:jar:3.17.1-GA:compile
INFO] | +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:jar:1.0.0.Final:compile
INFO] | +- dom4j:dom4j:jar:1.6.1:compile
INFO] | +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile
INFO] | +- org.hibernate:hibernate-core:jar:4.1.9.Final:compile
INFO] | | \- antlr:antlr:jar:2.7.7:compile
INFO] | \- org.hibernate.common:hibernate-commons-annotations:jar:4.0.1.Final:compile
INFO] +- commons-dbcp:commons-dbcp:jar:1.2.2:compile
INFO] | \- commons-pool:commons-pool:jar:1.3:compile
INFO] +- mysql:mysql-connector-java:jar:5.1.27:compile
INFO] +- javax.servlet:javax.servlet-api:jar:3.0.1:provided
INFO] \- org.hsqldb:hsqldb:jar:2.2.8:compile
INFO] ------------------------------------------------------------------------
INFO] BUILD SUCCESS
答案 0 :(得分:0)
就我而言,我在使用Eclipse IDE时遇到此错误,并通过更正运行配置使用的JRE版本来修复错误。