gpt4 book ai didi

java - testException = [null] 与 Hibernate 和 SpringMVC

转载 作者:太空宇宙 更新时间:2023-11-04 09:10:04 24 4
gpt4 key购买 nike

我正在尝试运行以下测试。在本例中,只是一个空白测试来尝试一下,但由于某种原因它不起作用。

@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;

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() {
// Trying to run this empty test
}
}

运行时得到以下信息:

INFO: Began transaction (1) for test context [DefaultTestContext@16fb356 testClass = UserHibernateDaoTest, testInstance = ar.edu.itba.paw.persistence.UserHibernateDaoTest@6bc248ed, testMethod = testCreate@UserHibernateDaoTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@224b4d61 testClass = UserHibernateDaoTest, locations = '{}', classes = '{class ar.edu.itba.paw.persistence.TestConfig}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@23a9ba52]; rollback [true]
Jan 15, 2020 1:17:10 PM org.springframework.test.context.transaction.TransactionContext endTransaction
INFO: Rolled back transaction for test context [DefaultTestContext@16fb356 testClass = UserHibernateDaoTest, testInstance = ar.edu.itba.paw.persistence.UserHibernateDaoTest@6bc248ed, testMethod = testCreate@UserHibernateDaoTest, testException = java.lang.IllegalArgumentException: Unknown entity: ar.edu.itba.paw.models.User, mergedContextConfiguration = [MergedContextConfiguration@224b4d61 testClass = UserHibernateDaoTest, locations = '{}', classes = '{class ar.edu.itba.paw.persistence.TestConfig}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]].

如果将 Rollback() 更改为 Rollback(false) 我得到:

 RollbackException: Transaction marked as rollbackOnly

有什么想法可以解决这个问题吗?

如果您觉得有必要,我在 this question. 中发布了我的 TestConfig

最佳答案

Spring 没有找到你的 User 类作为实体。所以它不知道 User 是一个实体。需要在TestConfig类中添加实体包进行组件扫描。

@ComponentScan({"src.main.java.ar.edu.itba.paw.persistence", "ar.edu.itba.paw.models" })

关于java - testException = [null] 与 Hibernate 和 SpringMVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59757705/

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