Why can't I test my project with JUnit 4?

I am using Spring 3, Hibernate 3 in my project. But I can not check it with JUnit 4.

Here is my test class:

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"file:WebRoot/WEB-INF/applicationContext.xml"}) public class UserDaoTestCase extends AbstractJUnit4SpringContextTests { @Autowired private UserDao userDao; @Test public void testSimple() { // do some tests... } // setters and getters public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } } 

I run this test and I always get this exception:

java.lang.IllegalStateException: Failed to load ApplicationContext in org.springframework.test.context.TestContext.getApplicationContext (TestContext.java:308) in org.springframework.test.context.support.DependencyInjectionenestenenestenenenestenenestenenestenenestenenestenenestenenestenenestenenestestenenestenenestenenestestenenestestenenestestenenestenenestenenestenenestenenestenenestestenenestenenenestenenenestenenestestenenestenenestenenenestestenenestenenenestenenestestenenestenenenestenenenestenenestenenestenenestenenestenenestenenestenenenestestenenestenenestenenestenenestenenestenenestenenestenenestenenest in org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance (DependencyInjectionTestExecutionListener.java:75) in org.springframework.test.context.TestContextmanager.prepareTestInstancefc. .SpringJUnit4ClassRunner.createTest (SpringJUnit4ClassRunner.java:220) in org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall.nable.jrun.jr java: 15) in org.springframework.test.context.junit4.SpringJUnit4ClassRunner.met hodBlock (SpringJUnit4ClassRunner.java:303) in org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild (SpringJUnit4ClassRunner.java:240) in org.junit.runners.BlockJUnitJununRunnerCLunChlass .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) org.junit.runners.ParentRunner.access $ 000 (ParentRunner.java:42) at org.junit.runners.ParentRunner $ 2.valuation (ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClass evaluate (RunBeforeTestClassCallbacks.java:61) in org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate (RunAfterTestClassCallbacks.java:70) on org.junit.runners.ParentRunner.nerunava .springframework.test.context.junit4.SpringJUnit4ClassRunner.run (SpringJUnit4ClassR unner.java:180) in org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (JUnit4TestReference.java:49) in org.eclipse.jdt.internal.junit.runner.TestExecution.run (TestExecution.ava 38) in org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java-00-0067) in org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java .eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (RemoteTestRunner.javahaps90) in org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner.java:197) Reason: org.spr beans.factory.BeanCreationException: Error creating bean with name "sessionFactory" defined in URL [file: WebRoot / WEB-INF / applicationContext.xml]: init method call failed; The nested exception is org.hibernate.HibernateException: Failed to get the standard bean factory check in org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean (AbstractAutowireCapableBeanFutory.jut.fore.bab.opeb.anopebutanfaboryupputingbutanfopebutanupportingbutanfopebutanupportingbutanupportingbutanfopebutanupportbutanfopebutanfaborybutanfaborybutanupportbutanfactorybowanuterfaborybutanfaborybowanuterfaborybutanfabpowerbutanfopebutanupperbutanfactorybutan doCreateBean (AbstractAutowireCapableBeanFactory.javaPoint19) in org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean (AbstractAutowireCapableBeanFactory.java:456) in org.sprebbf.foryboryfourforyforyboryfourforyfory ) in org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton (DefaultSingletonBeanRegistry.java:222) in org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (AbstractBeanFactory.java.org) factory.support.AbstractBeanFactory.get Bean (AbstractBeanFactory.java:190) in org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons (DefaultListableBeanFactory.javacla5757) in org.springframework.context.support.AbstractApplicationConlicationIconlicationIconlicationIconlicationIconlicationIconlicationIconlicationIconlicationIconlicationIconlicationfonecl .springframework.context.support.AbstractApplicationContext.refresh (AbstractApplicationContext.java:425) in org.springframework.test.context.support.AbstractGenericContextLoader.loadContext (AbstractGenericContextLoader.java:84) in org.sertext.otext.otext.otext.otext.otext.otextfotextfotextfotexttextupportinguppertextfoctertextstotextfotexttextupportinguppertextfotextfotexttextloadstontextfesttextloadstontextfesttextuppestinguppeterfesttextupotexttextloadstontextfesttextuppestinguppercontextercometerfesttextcontexteroperfesttextcontexteroperfesttextfoctortextuppestingcontexteroperfesttextupportinguppercontexteroecertextopertextgroundtextercodefloadtextloadtextfectexttexterloadpontextfoctertextopotextfoctortextupportinguppertextpontextloadheads .loadContext (AbstractGenericContextLoader.java:1) in org.springframework.test.context.TestContext.loadApplicationContext (TestContext.java:280) in org.springframework.test.context.TestContext.getApplicationContext (TestContext.ava ... 24 more Called: org.hibernate.HibernateException: unable to get get standard u bean check factory in org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL (BeanValidationActivator.java:127) in org.hibernate.cfg.Configuration.applyBeanValidationConstraintsOnDDL (Configuration.jfc.fava.Ifc. (Configuration.java:1654) in org.hibernate.cfg.Configuration.secondPassCompile (Configuration.java:1445) in org.hibernate.cfg.Configuration.buildMappings (Configuration.java:1375) in org.springframework.orm.hibernate3. LocalSessionFactoryBean.buildSessionFactory (LocalSessionFactoryBean.java:717) in org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet (AbstractSessionFactoryBean.java:211) in org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods (AbstractAutowireCapableBeanFactory.java:1469) in org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean (AbstractAutowireCapableBeanFactory.java:14 09) ... 37 more Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native method) in sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:39) in sun.reflectccess.legfpllccess. invoke (DelegatingMethodAccessorImpl.java:25) in java.lang.reflect.Method.invoke (Method.java//97) in org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL (BeanValidationActivator.java:118) ... 45 more : org.hibernate.HibernateException: it is not possible to create a default ValidatorFactory org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory (TypeSafeActivator.javahaps83) in org.hibernate.cfg.beanvalid.afeivateyafeeplyafafe ) ... 50 more Caused by: javax.validation.ValidationException: Unable to create config ation. in javax.validation.Validation $ GenericBootstrapImpl.configure (Validation.java:272) in javax.validation.Validation.buildDefaultValidatorFactory (Validation.java:111) in org.hibernate.cfg.beanvalidation.TypeAavaViveAtiveAtiveAtafeFa ) ... 51 more Caused by: java.lang.NullPointerException at java.util.ResourceBundle.getBundle (ResourceBundle.java:960) in org.hibernate.validator.engine.ResourceBundleMessageInterpolator.loadBundle (ResourceBundleMessageInterolol. hibernate.validator.engine.ResourceBundleMessageInterpolator.getFileBasedResourceBundle (ResourceBundleMessageInterpolator.java:182) in org.hibernate.validator.engine.ResourceBundleMessageInterpolator. (ResourceBundleMessageInterpolator.java:81) in org.hibernate.validator.engine.ResourceBundleMessageInterpolator. (ResourceBundleMessageInterpolator.java:73) in org.hibernate.validator.engine.ConfigurationImpl. (ConfigurationImpl.java:57) in org.hibernate.validator.HibernateValidator.createGenericConfiguration (HibernateValidator.java:43) in javax.validation.Validation $ GenericBootstrapImpl.configure (Validation.java:269) ... 53 more

My IDE is MyEclipse 9, and I use the JavaEE 6 libraries.

I think my applicationContext.xml is fine, "because it works fine when I run this project in tomcat, no exceptions.

Here is my SessionFactory bean in applicationContext.xml:

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="c3p0DataSource" /> <property name="packagesToScan"> <list> <value>com.myproject.entity</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.connection.provider_class"> org.hibernate.connection.C3P0ConnectionProvider </prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> 

How can I run tests using JUnit 4 correctly?

+4
source share
5 answers

I expect you to have a path to application-context.xml as Spring sees it. At first I tried to reset WebRoot / .

I would bind Spring sources to my project and hook in the debugger, breaking it:

org.springframework.test.context.TestContext.getApplicationContext (TestContext.java:308)

In it, you will see where it loads the application context, and you can use the interactive expression executable to try several different lines to find out what it can and cannot see.

I would also try this:

 @ContextConfiguration(locations = { "classpath*:applicationContext*.xml" }) 

This works in my project where the files are in WEB-INF / classes in the deployment.

+6
source

Are you really using your application context for testing?

I would change the approach to using a different application context for tests (for example, to determine the sources of test data, etc.) and put it along with your other test resources. Then a simple search will be performed as shown below.

 @ContextConfiguration("/path/to/my/test-context.xml") 

In your example, you must make sure that the path is correct! The URL of your file should be moved to a folder relative to the project root folder. Is there an application context there?

+1
source
 @ContextConfiguration(locations={"file:WebRoot/WEB-INF/applicationContext.xml"}) 

I think you will not find your Spring configuration config file from file: //. reinstall it.

0
source

I also had this problem, in my case the problem was loading the xml application configuration file. This was not a correctly defined application context. To be precise, there was no xmlns namespace: context = "http://www.springframework.org/schema/context".

0
source

I have the same problem when I update both src / test / resources / ApplicationResource.properties and src / main / resources / ApplicationResource.proerties.

And I updated POM.xml with context and test dependency

 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework-version}</version> </dependency> 

Src / test / resources / ApplicationContext.xml file uploaded successfully.

My problem is resolved.

0
source

Source: https://habr.com/ru/post/1389663/


All Articles