gpt4 book ai didi

java - JUnit 测试(使用 Spring MVC 和 Hibernate): IllegalArgumentException: Unknown entity

转载 作者:行者123 更新时间:2023-11-30 05:19:46 25 4
gpt4 key购买 nike

我正在尝试做一些 JUnit 持久性测试。我正在使用 Spring MVC 和 Hibernate。

我的 TestConfig 文件如下所示:

@ComponentScan({"src.main.java.ar.edu.itba.paw.persistence", })
@Configuration
public class TestConfig {

@Bean
public DataSource dataSource() {
final SimpleDriverDataSource ds = new SimpleDriverDataSource();
ds.setDriverClass(JDBCDriver.class);
ds.setUrl("jdbc:hsqldb:mem:paw");
ds.setUsername("ha");
ds.setPassword("");
return ds;
}

@Bean
public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setPackagesToScan("src.main.java.ar.edu.itba.paw.models");
factoryBean.setDataSource(dataSource());
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
factoryBean.setJpaVendorAdapter(vendorAdapter);
final Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.search.default.directory_provider", "filesystem");
properties.setProperty("hibernate.search.default.indexBase", "lucene/indexes");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("format_sql", "true");
factoryBean.setJpaProperties(properties);
return factoryBean;
}



@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
}

这是我尝试运行的测试。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
@Transactional
public class UserHibernateDaoTest {

private static final long USERID = 1;
private static final long NONEXISTENTUSERID = -1;
private static final String FIRSTNAME = "TestFirstName";
private static final String LASTNAME = "TestLastName";
private static final String EMAIL = "test1@mail.com";
private static final String PASSWORD = "TestPassword";
private static final String PHONENUMBER = "0000000";
private static final String ROLE = "USER";

@PersistenceContext
private EntityManager em;

@Autowired
private UserHibernateDao userHibernateDao;
private JdbcTemplate jdbcTemplate;

@Before
@Transactional
public void setUp() {
this.userHibernateDao = new UserHibernateDao();
User u;
u = new User();
u.setUserid(123);
u.setFirstName(FIRSTNAME);
u.setLastName(LASTNAME);
u.setEmail(EMAIL);
u.setPassword(PASSWORD);
u.setPhoneNumber(PHONENUMBER);
u.setRole(ROLE);
em.persist(u);

}

@Rollback
@Test
public void testCreate() {
// just trying to run this empty test
}
}

问题是我什至无法运行那个简单的空测试,因为我遇到了几个异常,最后一个是:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ar.edu.itba.paw.persistence.UserHibernateDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

关于如何解决这个问题有什么想法吗?

最佳答案

在进行测试时,您必须提供完整的 spring 配置才能实例化您的测试类。

在您的情况下,您有一个 UserHibernateDao 类属性的 Autowiring ,但请注意当前为 spring 提供了一种装配此类的方法。当您在设置方法中直接实例化 UserHibernateDao 时。只需删除 Autowiring 就可以解决您的问题。

关于java - JUnit 测试(使用 Spring MVC 和 Hibernate): IllegalArgumentException: Unknown entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59754882/

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