Announcement Announcement Module
Collapse
No announcement yet.
HSQL + Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HSQL + Spring

    Hello all I want add HSQL database for tests, I have this

    and this test

    PHP Code:
    package com.epos;

    import com.epos.kernel.dao.HeroDao;
    import com.epos.kernel.model.Choice;
    import com.epos.kernel.model.Hero;
    import junit.framework.TestCase;
    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 org.springframework.util.Assert;

    import java.util.List;

    @
    RunWith(SpringJUnit4ClassRunner.class)
    @
    ContextConfiguration(locations={"classpath:applicationContext.xml"})
    public class 
    TestHeroDaoImpl extends TestCase {
        private 
    HeroDao dao;
        
    Hero user = new Hero();

        @
    Autowired
        
    public void setDao(HeroDao dao) {
            
    this.dao dao;
        }

        @
    Test
        
    public void testSaveHero() {
            
    user.setName("unit_test");
            
    user.setChoice(Choice.RIGHT);
            
    dao.saveHero(user);
        }



    whet I connect real postgress database, test work well, but if I include this testApplicationContext.xml

    PHP Code:
    <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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"

    default-autowire="byName">
        <
    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:epos"/>
            <
    property name="username" value="sa"/>
            <
    property name="password" value=""/>
        </
    bean>

        <
    bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <
    property name="dataSource" ref="dataSource"/>
            <
    property name="hibernateProperties">
              <
    value>
                
    hibernate.dialect=org.hibernate.dialect.HSQLDialect
                hibernate
    .format_sql=true
                hibernate
    .hbm2ddl.auto=create
                hibernate
    .show_sql=true
                use_sql_comments
    =true
              
    </value>
            </
    property>
        </
    bean>

    </
    beans
    I have got exception

    PHP Code:
    ERROR TestContextManager         Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1a1c42fto prepare test instance [null(com.epos.TestHeroDaoImpl)]
    org.springframework.beans.factory.BeanCreationExceptionError creating bean with name 'com.epos.TestHeroDaoImpl'Autowiring of methods failednested exception is org.springframework.beans.factory.BeanCreationExceptionCould not autowire method: public void com.epos.TestHeroDaoImpl.setDao(com.epos.kernel.dao.HeroDao); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionExceptionNo unique bean of type [com.epos.kernel.dao.HeroDaois definedUnsatisfied dependency of type [interface com.epos.kernel.dao.HeroDao]: expected at least 1 matching bean
        at org
    .springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:254)
        
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:966)
        
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:325)
        
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:127)
        
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85)
        
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:277)
        
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95)
        
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139)
        
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61)
        
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:54)
        
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
        
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
        
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:52)
        
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
        
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:94)
        
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:192)
        
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:64)
        
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        
    at java.lang.reflect.Method.invoke(Method.java:597)
        
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
    Caused byorg.springframework.beans.factory.BeanCreationExceptionCould not autowire method: public void com.epos.TestHeroDaoImpl.setDao(com.epos.kernel.dao.HeroDao); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionExceptionNo unique bean of type [com.epos.kernel.dao.HeroDaois definedUnsatisfied dependency of type [interface com.epos.kernel.dao.HeroDao]: expected at least 1 matching bean
        at org
    .springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:536)
        
    at org.springframework.beans.factory.annotation.InjectionMetadata.injectMethods(InjectionMetadata.java:116)
        
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:251)
        ... 
    21 more
    Caused by
    org.springframework.beans.factory.NoSuchBeanDefinitionExceptionNo unique bean of type [com.epos.kernel.dao.HeroDaois definedUnsatisfied dependency of type [interface com.epos.kernel.dao.HeroDao]: expected at least 1 matching bean
        at org
    .springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:614)
        
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:498)
        ... 
    23 more 
    what need to change for fix this?

  • #2
    The error is:

    Code:
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.epos.kernel.dao.HeroDao] is defined: Unsatisfied dependency of type [interface com.epos.kernel.dao.HeroDao]: expected at least 1 matching bean
    How is this related to HSQL? It's missing a bean definition or cannot find a unique one.

    Comment


    • #3
      how can I fix this? I think this situation will be fixing after create mock object. "HeroDao" Do you know another method for fix ?

      Comment


      • #4
        I have changed my test, with mock

        PHP Code:
        package com.epos;

        import com.epos.kernel.dao.HeroDao;
        import com.epos.kernel.model.Choice;
        import com.epos.kernel.model.Hero;
        import junit.framework.TestCase;
        import org.junit.Before;
        import org.junit.Test;
        import org.junit.runner.RunWith;
        import org.springframework.test.context.ContextConfiguration;
        import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
        import org.springframework.util.Assert;
        import static org.easymock.EasyMock.*;


        import java.util.List;

        @
        RunWith(SpringJUnit4ClassRunner.class)
        @
        ContextConfiguration(locations={"testApplicationContext.xml"})
        public class 
        TestHeroDaoImpl extends TestCase {
            private 
        HeroDao dao;
            private 
        Hero user

            @
        Before
            
        public void setUP(){
                
        dao createMock(HeroDao.class);
                
        user createMock(Hero.class);            
            }

            @
        Test
            
        public void testSaveHero() {
                
        user.setName("unit_test");
                
        user.setChoice(Choice.RIGHT);
                
        dao.saveHero(user);
            }

            @
        Test
            
        public void testGetHeroByName(){
                
        replay(dao);
                List<
        Object[]> res;
                
        res dao.getHeroByName("unit_test");
                
        Assert.notEmpty(res);
            }


        All test passed, but I get this exception into console:

        PHP Code:
        INFO  SchemaExport               schema export complete
        Exception in thread 
        "main" java.lang.NoClassDefFoundErrororg/unit/ssume$AssumptionViolatedException
            at org
        .springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:259)
            
        at org.springframework.test.context.junit4.SpringMethodRoadie$2.run(SpringMethodRoadie.java:194)
            
        at org.springframework.test.context.junit4.SpringMethodRoadie.runBeforesThenTestThenAfters(SpringMethodRoadie.java:235)
            
        at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:218)
            
        at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:192)
            
        at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:140)
            
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:151)
            
        at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61)
            
        at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:54)
            
        at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
            
        at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
            
        at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:52)
            
        at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
            
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:94)
            
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:192)
            
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:64)
            
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            
        at java.lang.reflect.Method.invoke(Method.java:597)
            
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
        Caused byjava.lang.ClassNotFoundExceptionorg.junit.Assume$AssumptionViolatedException
            at java
        .net.URLClassLoader$1.run(URLClassLoader.java:202)
            
        at java.security.AccessController.doPrivileged(Native Method)
            
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
            
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
            
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
            ... 
        21 more
        INFO  
        GenericApplicationContext  Closing org.springframework.context.support.GenericApplicationContext@1551d7fdisplay name [org.springframework.context.support.GenericApplicationContext@1551d7f]; startup date [Wed Dec 29 13:50:20 EET 2010]; root of context hierarchy
        INFO  
        DefaultListableBeanFactory Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@609959defining beans [dataSource,sessionFactory,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor]; root of factory hierarchy
        INFO  
        notationSessionFactoryBean Closing Hibernate SessionFactory
        INFO  
        SessionFactoryImpl         closing 
        help me please

        Comment


        • #5
          You're missing class. Make sure you have them in your classpath:
          Code:
          java.lang.ClassNotFoundException: org.junit.Assume$AssumptionViolatedException

          Comment


          • #6
            which class ? I was make mock my class, who are missing ?


            PHP Code:
            import java.util.List;

            @
            RunWith(SpringJUnit4ClassRunner.class)
            @
            ContextConfiguration(locations={"testApplicationContext.xml"})
            public class 
            TestHeroDaoImpl extends TestCase {

                private 
            HeroDao dao;
                private 
            Hero user = new Hero();


                @
            Before
                
            public void setUP(){
                    
            dao createMock(HeroDao.class);  
                }

                @
            Test
                
            public void testSaveHero() {
                    
            user.setName("unit_test");
                    
            user.setChoice(Choice.RIGHT);
                    
            dao.saveHero(user);
                }

                @
            Test
                
            public void testGetHeroByName(){
                    
            dao createMock(HeroDao.class);
                    List<
            Object[]> res;
                    
            res dao.getHeroByName("unit_test");
                    
            Assert.notEmpty(res);
                }


            Comment


            • #7
              For starters don't mix JUnit 3 and JUnit4 (Don't extend testcase).

              Also why do you want to create a mock, with that your applicationcontext is useless. So either do a mock and don't use the JUnit testrunner or don't use a mock and run an integration test.

              Also you don't include the testApplicationContext you ONLY load the testApplicationContext and that obviously doesn't contain your dao. You should load BOTH xml files.

              Comment

              Working...
              X