当前位置: 首页 > 知识库问答 >
问题:

执行TestNG测试时出错

华季同
2023-03-14

我有一个应用程序(使用注释的Spring 4 MVC Hibernate 4 MySQL Maven集成示例),使用基于注释的配置将Spring与Hibernate集成,但运行测试时出错!

 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory fr.telecom.iot.dao.AbstractDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class fr.telecom.iot.configuration.HibernateTestConfiguration: Invocation of init method failed; nested exception is java.lang.AbstractMethodError

上课时间到了

@Configuration
@EnableTransactionManagement
@ComponentScan({ "fr.telecom.iot.dao" })
public class HibernateTestConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "fr.telecom.iot.model" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }

    @Bean(name = "dataSource")
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.h2.Driver");
        dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
        dataSource.setUsername("sa");
        dataSource.setPassword("");
        return dataSource;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        properties.put("hibernate.hbm2ddl.auto", "create-drop");
        return properties;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(s);
        return txManager;
    }
}

下面是即将到来的考验:

public class DeviceDaoImplTest extends EntityDaoImplTest{

    @Autowired
    DeviceDao deviceDao;

    @Override
    protected IDataSet getDataSet() throws Exception{
        IDataSet dataSet = new FlatXmlDataSet(this.getClass().getClassLoader().getResourceAsStream("Device.xml"));
        return dataSet;
    }


    @Test
    public void findById(){
        Assert.assertNotNull(deviceDao.findById(1));
        Assert.assertNull   (deviceDao.findById(3));
    }
}


@ContextConfiguration(classes = { HibernateTestConfiguration.class })
public abstract class EntityDaoImplTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    DataSource dataSource;

    @BeforeMethod
    public void setUp() throws Exception {
        IDatabaseConnection dbConn = new DatabaseDataSourceConnection(
                dataSource);
        DatabaseOperation.CLEAN_INSERT.execute(dbConn, getDataSet());
    }

    protected abstract IDataSet getDataSet() throws Exception;

}

这是pom。xml文件:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <modelVersion>4.0.0</modelVersion>
    <groupId>fr.telecom.iot</groupId>
    <artifactId>DevicesIOT</artifactId>
    <packaging>war</packaging>
    <version>1.0.0</version>
    <name>DevicesIOT</name>

    <properties>
        <springframework.version>4.0.6.RELEASE</springframework.version>
        <hibernate.version>5.0.4.Final</hibernate.version>
        <mysql.version>5.1.31</mysql.version>
        <joda-time.version>2.3</joda-time.version>
        <testng.version>6.9.4</testng.version>
        <mockito.version>1.10.19</mockito.version>
        <h2.version>1.4.187</h2.version>
        <dbunit.version>2.2</dbunit.version>
    </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.4.Final</version>
            <scope>runtime</scope>
        </dependency>

        <!-- Java Transaction API -->
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>

        <!-- jsr303 validation -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.3.Final</version>
        </dependency>

        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!-- Joda-Time -->      
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>${joda-time.version}</version>
        </dependency>

        <!-- To map JodaTime with database type -->         
        <dependency>
            <groupId>org.jadira.usertype</groupId>
            <artifactId>usertype.core</artifactId>
            <version>3.0.0.CR1</version>
        </dependency>

        <!-- Servlet+JSP+JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

         <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>

         <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
      <dependency>
         <groupId>org.apache.commons</groupId>
             <artifactId>commons-lang3</artifactId>
             <version>3.4</version>
        </dependency>


        <!-- Testing dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springframework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>${mockito.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>dbunit</groupId>
            <artifactId>dbunit</artifactId>
            <version>${dbunit.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <warSourceDirectory>src/main/webapp</warSourceDirectory>
                        <warName>DevicesIOT</warName>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <finalName>DevicesIOT</finalName>
    </build>    
</project>

共有1个答案

壤驷棋
2023-03-14

看起来库版本有冲突。请检查您使用的SpringHibernate版本应替换为

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

看看这个和这个

 类似资料:
  • 我试图在Eclipse中使用Selenium运行TestNG。 当类文件作为TestNG测试运行时,我得到的测试Run=0。 问题会是什么? 我有testNg插件 testng。xml文件: 类: WebDriverTest类: BrowserInstance类: 注: 当我将testing.xml文件作为TestNG Suite运行时,结果是: 当我运行. java文件作为TestNG测试时,结

  • 类TestParallel.FirstTestClass线程ID:22名称:TestNG 类TestParallel.SecondTestClass线程ID:23名称:TestNG 类TestParallel.TestSetup线程ID:23名称:TestNG java.lang.NullPoInterException位于TestParallel.TestSetup.OnTestFailure(

  • 试着用TestNG运行我所有的cucumber测试。然而,我的套房没有找到。 当我将TestNG.xml文件作为TestNG运行时,未找到如下所示的测试

  • 我已经使用selenium创建了测试用例,之前我可以使用maven(从命令行和eclipse)执行我的测试用例。但是现在它不起作用了。请帮助 请在下面找到控制台日志:(运行为- 我没有得到任何错误,它成功地构建了一个项目。但是跳过所有测试用例 请找到我的pom。xml配置如下: 请找到E2E。xml如下:

  • 我试图通过testng运行简单的Cucumber/Java测试。xml。 所以,我有testng。xml: 我用的是runner。类,在其中我将路径/选项/etc设置为功能文件、步骤和报告: 但是当我运行testng时。xml作为TestNG套件,它: 1) 通过我自己的设想, 但是 我做错了什么?

  • 我一个类有5到6个方法,想在不同的节点上并行运行方法,我有网格2设置,里面有4个节点。 下面是我的测试。xml 我有一个测试工具,它初始化了login、common和utils类 在我的测试类中,我扩展了测试工具,在@Beforemethod中,我调用了inilze方法 如果我运行测试,我会看到以下问题 两个浏览器在每个节点中打开一个,但只有一个浏览器启动应用程序,另一个不启动。 如果我遗漏了什么