`
icomparator
  • 浏览: 18591 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Spring完成简洁Junit4测试

 
阅读更多
  导入Spring test测试jar包
          spring-test-3.2.0.RELEASE.jar
  

  package springTest;
  
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  
  import com.myapps.service.IUserService;
  
  //   如果不写"/"代表从(SpringTest)本测试类当前包作为根路径。
  //   写"/"代表从classpath的根查找
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("/applicationContext.xml")
  public class MySpringTest {
  
  	@Autowired
  	private IUserService userService;
  	
  	@Test
  	public void test(){
  		System.out.println(userService);
  	}
  	
  }
  

 

 

  抛出以下异常:
  
  java.lang.IllegalStateException: Failed to load ApplicationContext
  	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
  	at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:103)
  	at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:73)
  	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313)
  	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
  	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
  	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
  	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
  	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
  	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
  	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
  	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
  	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
  	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
  	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
  	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
  	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
  	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
  	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
  	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
  	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
  	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [applicationContext-service.xml]: Cannot resolve reference to bean 'userDao' while setting bean property 'userDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in class path resource [applicationContext-dao.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to get the default Bean Validation factory
  	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
  	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
  	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1391)
  	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1132)
  	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
  	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
  	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
  	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
  	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
  	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
  	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
  	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
  	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
  	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:106)
  	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:57)
  	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
  	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
  	at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)
  	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
  	... 24 more
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in class path resource [applicationContext-dao.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to get the default Bean Validation factory
  	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
  	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
  	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1391)
  	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1132)
  	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
  	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
  	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
  	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
  	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
  	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
  	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
  	... 42 more
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to get the default Bean Validation factory
  	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
  	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
  	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
  	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
  	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
  	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
  	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
  	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
  	... 52 more
  Caused by: org.hibernate.HibernateException: Unable to get the default Bean Validation factory
  	at org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL(BeanValidationActivator.java:127)
  	at org.hibernate.cfg.Configuration.applyBeanValidationConstraintsOnDDL(Configuration.java:1704)
  	at org.hibernate.cfg.Configuration.applyConstraintsToDDL(Configuration.java:1654)
  	at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1445)
  	at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1375)
  	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:720)
  	at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:188)
  	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
  	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
  	... 59 more
  Caused by: java.lang.reflect.InvocationTargetException
  	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  	at java.lang.reflect.Method.invoke(Unknown Source)
  	at org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL(BeanValidationActivator.java:118)
  	... 67 more
  Caused by: org.hibernate.HibernateException: Unable to build the default ValidatorFactory
  	at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:383)
  	at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:109)
  	... 72 more
  Caused by: javax.validation.ValidationException: Unable to instantiate Configuration.
  	at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:272)
  	at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)
  	at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:380)
  	... 73 more
  Caused by: java.lang.NullPointerException
  	at java.util.ResourceBundle.getBundle(Unknown Source)
  	at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.loadBundle(ResourceBundleMessageInterpolator.java:202)
  	at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.getFileBasedResourceBundle(ResourceBundleMessageInterpolator.java:182)
  	at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.<init>(ResourceBundleMessageInterpolator.java:81)
  	at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.<init>(ResourceBundleMessageInterpolator.java:73)
  	at org.hibernate.validator.engine.ConfigurationImpl.<init>(ConfigurationImpl.java:57)
  	at org.hibernate.validator.HibernateValidator.createGenericConfiguration(HibernateValidator.java:43)
  	at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:269)
  	... 75 more
  

 

 

 

  [ 解决办法 ]
javax.persistence.validation.mode默认情况下是auto的,就是说如果不设置的话它是会自动去你的classpath下面找一个bean-validation包,但是找不到,所以beanvalitionFactory错误。由于javax.persistence.validation.mode的属性值默认是auto,所以会出错。在hibernate.cfg.xml里将javax.persistence.validation.mode设置为none,就可以避免出错了。 	<property name="javax.persistence.validation.mode">none</property>
如果hibernate的主配置文件交由spring管理,则在相应的位置修改即可。
关于Bean Validation 的快速入门可以参考前辈们写的:
http://www.iteye.com/news/15265
http://developer.51cto.com/art/201104/253257.htm
  

 

分享到:
评论

相关推荐

    spring4+JUnit简单测试

    《Spring4与JUnit的简单测试实践》 在Java开发领域,Spring框架以其强大的功能和灵活性深受开发者喜爱。Spring4作为其一个重要的版本,引入了许多改进和优化,使得测试变得更加便捷。而JUnit作为Java单元测试的事实...

    junit4测试jar包

    10. **扩展性与兼容性**:JUnit4设计得非常开放,可以通过实现`Runner`接口来扩展其功能,例如SpringJUnit4ClassRunner可以结合Spring框架进行测试。同时,JUnit4与大多数IDE和构建工具(如Maven、Gradle)良好集成...

    spring3 and junit4

    在Java开发领域,Spring框架和JUnit测试工具是不可或缺的重要组成部分。Spring3.2.8是Spring框架的一个稳定版本,它提供了丰富的功能,包括依赖注入、AOP(面向切面编程)、MVC(模型-视图-控制器)以及数据访问等。...

    Junit4测试使例

    首先,JUnit4引入了注解(Annotations)的概念,使得测试类和测试方法的声明更加简洁。例如,`@Test`注解标记测试方法,表示该方法将被测试框架执行。此外,`@Before`和`@After`注解分别用于定义在每个测试方法执行...

    spring-Test,Junit4 jar,和测试代码

    JUnit4则提供了诸如`@Test`、`@Before`、`@After`等注解,使得测试用例的编写更加简洁。`@Test`标记测试方法,`@Before`和`@After`分别在每个测试方法之前和之后执行,常用于初始化和清理工作。 在Spring Test与...

    27. Spring Boot Junit单元测试【从零开始学Spring Boot】

    在Java世界里,JUnit是最常用的单元测试框架,它提供了一套简洁的API来编写和运行测试用例。 Spring Boot提供了对JUnit的内置支持,使得我们能够方便地对Spring组件进行测试。首先,我们需要在项目的`pom.xml`或`...

    spring3 + mybatis3 + junit4 可运行项目示例

    这是一个基于Spring3、MyBatis3和JUnit4的可运行项目示例,旨在提供一个完整的、可测试的Java Web应用程序框架。这个项目的核心是利用Spring作为应用的ioc(Inversion of Control,控制反转)和aop(Aspect Oriented...

    struts2_spring3.0_Junit4.7_Maven2.2.1_整合运行说明_培训.pdf )

    JUnit4.7版本引入了注解特性,使得编写测试用例变得更加简洁。通过JUnit,开发者可以方便地编写针对Struts2 Action和Spring Service层的单元测试,从而确保代码的质量。 #### Maven2.2.1 Maven是一个项目管理和理解...

    通过spring管理的web工程的junit单元测试方法四

    Spring TestContext框架提供了一个统一的入口点,允许我们在JUnit测试中加载Spring应用上下文。这可以通过继承`@RunWith(SpringRunner.class)`注解的测试类实现,或者使用`@SpringBootTest`注解直接启动应用上下文...

    单元测试利器JUnit4

    JUnit4相较于早期版本引入了许多改进,如注解、参数化测试和测试套件等,使得测试代码更加简洁和灵活。 2. 注解(Annotations): JUnit4的核心特点是广泛使用注解,这使得测试类和方法的声明更清晰。例如: - `@...

    SSM中进行单元测试Junit4+spring-test所需jar包

    Junit4是Java领域广泛使用的单元测试框架,它提供了一套丰富的注解,使得编写测试用例变得更加简洁和直观。例如,`@Test`注解标记测试方法,`@Before`和`@After`用于定义每个测试前后的准备工作和清理工作,还有`@...

    springjunit

    SpringJUnit就是一种结合了Spring框架和JUnit测试工具的解决方案,它为基于Spring的应用程序提供了强大的单元测试支持。下面我们将深入探讨SpringJUnit的相关知识点。 首先,JUnit是Java编程语言中最常用的单元测试...

    activti实例带完整JUnit4测试

    在Activiti实例中,开发者可以使用JUnit4编写单元测试,检查流程的每个步骤是否按预期执行,如任务的创建、分配、完成等。 3. **Spring MVC**:Spring MVC使得开发者可以使用声明式的方式处理HTTP请求,通过控制器...

    单元测试案例junit +spring mvc +springboot

    JUnit作为Java领域最流行的单元测试框架,与Spring MVC和Spring Boot的集成使得开发者能够更高效地进行测试驱动开发(TDD)或行为驱动开发(BDD)。下面将详细介绍这个主题中的相关知识点。 1. JUnit JUnit是一个...

    Junit单元测试(很经典)

    在Java世界里,Junit是最广泛使用的单元测试框架,它为Java程序员提供了简洁易用的API来进行单元测试。本篇文章将深入探讨Junit的基础、中级和高级应用。 ### 基础篇 1. **安装与配置**:Junit通常作为Maven或...

    spring-aop demo及junit测试

    本教程将介绍两种常见的Spring AOP配置方法,并通过JUnit进行测试,确保其正确运行。 首先,我们来看看什么是AOP。AOP的核心思想是将关注点分离,即把横切关注点(如日志、事务管理等)从业务逻辑中解耦出来。...

    springboot 的junittest 单元测试的例子

    在Spring Boot项目中,JUnit测试是开发者进行单元测试的常用工具。JUnit是一个流行的Java编程语言的单元测试框架,它使得测试代码的编写变得简洁而高效。这篇博客文章(虽然描述为空,但可以从标题和标签推测内容)...

    Junit 测试

    JUnit 是Java编程语言中最广泛使用的单元测试框架,它为开发者提供了一种简洁、高效的方式来编写和运行测试用例,以验证代码的功能是否符合预期。本篇将深入探讨JUnit的使用以及在Spring MVC中的应用。 首先,我们...

    Junit测试 简单用例

    本实例将深入探讨Junit测试的基本用法,包括简单单元测试和综合测试的实现。 一、Junit简介 Junit是由Ernst Berg和Kent Beck共同创建的开源项目,其主要目标是提供一个易于使用的接口来编写针对Java代码的测试用例...

Global site tag (gtag.js) - Google Analytics