JUnit测试,出了什么问题?

时间:2016-11-12 16:34:20

标签: java junit

大家好我是初学者。我的测试类或配置有什么问题? 我正在使用JPA。应用程序的工作正常,但我不明白为什么无法在我的测试中加载ApplicationContext。 我的pom:

<?xml version="1.0" encoding="UTF-8"?>
<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>TestAppGrId</groupId>
    <artifactId>TestApp</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <!-- Generic properties -->
        <java.version>1.8</java.version>
        <!-- Web -->
        <jsp.version>2.2</jsp.version>
        <jstl.version>1.2</jstl.version>
        <servlet.version>3.1.0</servlet.version>

        <!-- Spring -->
        <spring-framework.version>4.3.3.RELEASE</spring-framework.version>

        <!-- Logging -->
        <logback.version>1.1.7</logback.version>
        <slf4j.version>1.7.21</slf4j.version>

        <!-- jackson json JSON Processing API -->
        <jackson.databind-version>2.2.3</jackson.databind-version>

        <!--Hibernate / JPA-->
        <hibernate-version>5.2.3.Final</hibernate-version>

        <!-- Spring Data -->
        <spring-framework.data.version>1.9.1.RELEASE</spring-framework.data.version>

        <!-- JUnit test -->
        <junit.version>4.12</junit.version>

        <!-- Quartz scheduling framework -->
        <quartz.scheduling.version>2.2.1</quartz.scheduling.version>

    </properties>

    <dependencyManagement>
        <!--all spring dependencies -->
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>4.3.3.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--bootstrap webjars.org-->
    <!--http://startbootstrap.com/template-overviews/modern-business/  this is theme-->
    <dependencies>
        <!-- Spring MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>

        <!-- Other Servlet Web dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <!--Servlet API-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp.version}</version>
            <scope>provided</scope>
        </dependency>

        <!--driver for connection to Posgres database -->
        <!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
        </dependency>

        <!-- Spring JDBC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- JUnit tests-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- Test Artifacts with Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-framework.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- Logging with SLF4J & LogBack -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
            <scope>runtime</scope>
        </dependency>

        <!--Contains org.springframework.mail.javamail-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- Spring MVC Mail Related Dependency -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>

        <!-- Spring REST jackson JSON Processing API -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.databind-version}</version>
        </dependency>

        <!-- Hibernate ORM-->
        <!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate-version}</version>
        </dependency>

        <!--Hibernate validator (contains @NotEmpty)-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${hibernate-version}</version>
        </dependency>

        <!-- Spring Data JPA -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.10.4.RELEASE</version>
        </dependency>

        <!--AOP. Need for Spring Data JPA-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring-framework.version}</version>
            <scope>compile</scope>
        </dependency>

        <!-- Quartz scheduling framework -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>${quartz.scheduling.version}</version>
        </dependency>

        <!-- Joda-Time - API uses in Spring Data-->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.7</version>
        </dependency>
        <!--Joda-Time integration with Hibernate. Save types of date and time -->
        <!--dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time-hibernate</artifactId>
            <version>1.3</version>
        </dependency-->

        <!-- DATE TIME FOR HIBERNATE 4.0+, for Hibernate <4.0 use joda-time-hibernate version 1.3 -->
        <dependency>
            <groupId>org.jadira.usertype</groupId>
            <artifactId>usertype.jodatime</artifactId>
            <version>2.0.1</version>
        </dependency>


        <!-- Support methods from google. For example 'Lists.newArrayList()'-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>

        <!--Generate metamodel classes ClassName_ -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>${hibernate-version}</version>
        </dependency>

    </dependencies>

    <build>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
        </plugins>

        <!--need to find configs in tests in package web-inf like
        @ContextConfiguration(locations = {"classpath:/config/application-context.xml"
        -->
        <testResources>
            <testResource>
                <directory>src/main/webapp/WEB-INF/config</directory>
            </testResource>
        </testResources>

    </build>

</project>

我的application-context.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:task="http://www.springframework.org/schema/task"
       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/context
          http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/data/jpa
            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
             http://www.springframework.org/schema/jdbc
              http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
               http://www.springframework.org/schema/task
                http://www.springframework.org/schema/task/spring-task.xsd">

    <!--find property file. See bean id='dataSource' for example ${jdbc.hsqldb.driverClass}-->
    <context:property-placeholder location="classpath:util.properties" />

    <!-- Datasource-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.postgresqldb.driverClass}" />
        <property name="url" value="${jdbc.postgresqldb.url}" />
        <property name="username" value="${jdbc.postgresqldb.username}" />
        <property name="password" value="${jdbc.postgresqldb.password}" />
    </bean>

    <!-- initialize Embedded DataSource.-->
    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="classpath:dbschema.sql"/>
        <jdbc:script location="classpath:test-data.sql"/>
    </jdbc:initialize-database>

    <!-- Java Mail Configuration -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="username" value="${java.mail.username}"/>
        <property name="password" value="${java.mail.password}"/>
        <property name="port" value="465"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.smtp.starttls.required">true</prop>
                <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
                <prop key="mail.smtp.host">${java.mail.host}</prop>
            </props>
        </property>
    </bean>

    <!-- Velocity Email Template Config Bean -->
    <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="resourceLoaderPath" value="/WEB-INF/email-templates/"/>
    </bean>

    <!-- REST template configuration -->
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>

    <!--Do not forget activate @Transactional JPA annotation with <annotation-driven/>-->
    <!-- JPA Persistence Context and EntityManager configuration -->
    <!-- EntityManagerFactory -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <!--all entities in this package-->
        <property name="packagesToScan" value="testapp.mvc.entities"/>
        <property name="dataSource" ref="dataSource" />
        <!--hibernate-->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
                <property name="generateDdl" value="true" />
                <property name="showSql" value="true" />
            </bean>
        </property>

        <!--hibernate-->
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL94Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <!--transaction in Spring -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <!--Activates various annotations to be detected in bean classes: Spring's @Required and @Autowired and so on-->
    <!--context:annotation-config/-->

    <!-- Need for Repository abstraction -->
    <jpa:repositories base-package="testapp.mvc.repository" entity-manager-factory-ref="entityManagerFactory"
                      transaction-manager-ref="transactionManager"/>

    <!-- Enable auditing in Spring Data -->
    <jpa:auditing auditor-aware-ref="auditorAwareBean"/>

    <!-- return user information -->
    <bean id="auditorAwareBean" class="testapp.mvc.auditor.AuditorAwareBean" />

    <!-- Quartz scheduling configuration -->
    <task:annotation-driven/>

    <!-- Quartz simple trigger -->
    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="simpleQuartzJob" />
        <property name="repeatInterval" value="3000" />
        <property name="startDelay" value="1000" />
    </bean>
    <!-- Quartz cron trigger -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="quartzCronJob"/>
        <property name="cronExpression" value="0/7 * * * * ?" />
        <!-- every 30 seconds (seconds, minutes, hours, day of month, month, day of week, year(optional)) -->
    </bean>

    <!-- Quartz job -->
    <bean id="simpleQuartzJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="simpleQuartzTask" />
        <property name="targetMethod" value="simpleTaskMethod" />
    </bean>
    <!-- Quartz cron job -->
    <bean id="quartzCronJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="cronQuartzTask" />
        <property name="targetMethod" value="cronTaskMethod" />
    </bean>

    <!-- Quartz simple task -->
    <bean id="simpleQuartzTask" class="testapp.mvc.quartz.QuartzTask" />

    <!-- Quartz cron task -->
    <bean id="cronQuartzTask" class="testapp.mvc.quartz.CronQuartzTask" />

    <!-- Quartz Scheduler -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="simpleQuartzJob" />
                <ref bean="quartzCronJob" />
            </list>
        </property>
        <property name="triggers">
            <list>
                <ref bean="simpleTrigger" />
                <ref bean="cronTrigger" />
            </list>
        </property>
    </bean>

    <!--ScheduleTask example. Use and @Component on ru.javastudy.mvcHtml5Angular.mvc.scheduling.ScheduleTask-->
    <!--<context:component-scan base-package="ru.javastudy.mvcHtml5Angular.mvc.scheduling"/>-->

    <!--or
   <bean id="scheduleTask" class="ru.javastudy.mvcHtml5Angular.mvc.scheduling.ScheduleTask"/>
   -->

    <!--End scheduling configuration -->

</beans>

我的服务类:

@Service("jpaCustomerService")
@Repository
@Transactional

public class CustomerServiceImpl implements CustomerService {
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public List<CustomersEntity> queryFindAllCustomersJPA() {
        System.out.println("ORMService queryfindAllUsersJPA is called");
        String query = "from CustomersEntity order by id";
        TypedQuery<CustomersEntity> typedQuery = entityManager.createQuery(query, CustomersEntity.class);
        return typedQuery.getResultList();
    }
}

我的考试班:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:mvc-config.xml", "classpath:application-context.xml"})
    public class CustomerServiceImplTest {

        @Qualifier("jpaCustomerService")
        @Autowired
        private CustomerServiceImpl customerService;

        //CustomerService queryFindAllCustomers TEST EXAMPLE
        @Test
        public void queryFindAllCustomersTest(){
            List<CustomersEntity> customers = customerService.queryFindAllCustomersJPA();
            Assert.assertNotNull(customers);
            for (CustomersEntity customersEntity : customers){
                System.out.printf("Customer id: "+ customersEntity.getId() + " Customer name: "+ customersEntity.getName());
            }
        }
}

我的问题是无法加载ApplicationContext:

-------------------------------------------------------------------------------
Test set: testapp.mvc.orm.CustomerServiceImplTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.734 sec <<< FAILURE!
queryFindAllCustomersTest(testapp.mvc.orm.CustomerServiceImplTest)  Time elapsed: 0.001 sec  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler [locations=[class path resource [resources/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@1693ff90]]] does not run in a WebApplicationContext but in: org.springframework.context.support.GenericApplicationContext@577bf0aa: startup date [Wed Nov 09 18:08:38 CET 2016]; root of context hierarchy
    at org.springframework.web.context.support.WebApplicationObjectSupport.getWebApplicationContext(WebApplicationObjectSupport.java:112)
    at org.springframework.web.context.support.WebApplicationObjectSupport.getServletContext(WebApplicationObjectSupport.java:128)
    at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.initPathExtensionStrategy(ResourceHttpRequestHandler.java:310)
    at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.afterSingletonsInstantiated(ResourceHttpRequestHandler.java:298)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:771)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    ... 31 more

1 个答案:

答案 0 :(得分:0)

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration
public class **{}
  

@WebAppConfiguration是一个类级别的注释,用于声明为集成测试加载的ApplicationContext应该是WebApplicationContext。   测试类上存在@WebAppConfiguration指示应使用Web应用程序根路径的默认值为测试加载WebApplicationContext。要覆盖默认值,请通过value()属性指定显式资源路径。

     

请注意,@ WebAppConfiguration必须与@ContextConfiguration结合使用,可以在单个测试类中,也可以在测试类层次结构中使用。