gpt4 book ai didi

java - Spring 在运行 TestNG 测试用例时加载 ApplicationContext 失败 - AutoProxyRegistrar.class 无法打开

转载 作者:行者123 更新时间:2023-11-30 07:11:37 24 4
gpt4 key购买 nike

我有一个使用 Hibernate ORM 的 Spring MVC 项目。当我尝试使用 TestNG 进行集成测试时,我收到 java.lang.IllegalStateException: Failed to load ApplicationContext。这是我的堆栈跟踪:

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.transaction.TestContextTransactionUtils.retrieveTransactionManager(TestContextTransactionUtils.java:163)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.getTransactionManager(TransactionalTestExecutionListener.java:361)
...
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [kz.agrotrade.hibernate.HibernateTestConfig]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/context/annotation/AutoProxyRegistrar.class] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:546)
...
Caused by: java.io.FileNotFoundException: class path resource [org/springframework/context/annotation/AutoProxyRegistrar.class] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
...
<小时/>

项目结构:

Project's test part structure

<小时/>

当我尝试执行此测试类时发生异常:

package kz.agrotrade.hibernate;

import kz.agrotrade.domain.User;
import kz.agrotrade.testutil.Common;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


public class HibernateUserRepositoryTest extends HibernateRepositoryTest {

@Autowired
HibernateUserRepository userRepository;

@Spy
private User superman;

@BeforeClass
public void setUp() {
MockitoAnnotations.initMocks(this);
superman = Common.getSuperman();
}

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

@Test
public void findByLogin() {
User user = userRepository.findByLogin("kent777");
Assert.assertNotNull(user);
Assert.assertEquals(user, superman);
}

}

HibernateRepositoryTest.java:

package kz.agrotrade.hibernate;


import org.dbunit.database.DatabaseDataSourceConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.annotations.BeforeMethod;

import javax.sql.DataSource;

@ContextConfiguration(classes = {HibernateTestConfig.class})
public abstract class HibernateRepositoryTest 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;

}

HibernateTestConfig.java

package kz.agrotrade.hibernate;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
@ComponentScan("kz.agrotrade")
public class HibernateTestConfig {

@Autowired
private Environment environment;

@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan("kz.agrotrade");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/agro-core");
dataSource.setUsername("agro");
dataSource.setPassword("agropass");
return dataSource;
}

private Properties hibernateProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.format_sql", "true");
return properties;
}

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

}

我不太确定是什么导致了这个问题。如何解决此错误?

最佳答案

作为先生。 Julien Herr 建议,我仅使用 Maven 运行测试,并遇到了 A ServletContext is required to configure default servlet Handling 错误。所以解决方案是在测试类中添加@WebAppConfiguration:

@WebAppConfiguration
@ContextConfiguration(classes = {HibernateTestConfig.class})
public abstract class HibernateRepositoryTest 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;

}

关于java - Spring 在运行 TestNG 测试用例时加载 ApplicationContext 失败 - AutoProxyRegistrar.class 无法打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39143960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com