gpt4 book ai didi

java - 坚持/提交在 Spring JPA JUnit 的测试环境中不起作用

转载 作者:行者123 更新时间:2023-12-03 07:18:48 25 4
gpt4 key购买 nike

我正在尝试设置基本的 JPA 插入测试。但数据库中没有保存任何内容。数据库是Postgresql。 Hibernate 用作持久性提供者。

提前非常感谢。

@Entity
public class Currency {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Integer id;

@Column
private String code;

@Column
private String name;
...
}

CRUD 类:

@Repository
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Transactional(propagation = Propagation.REQUIRED)
public class CRUDServiceBean implements CRUDService {

@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager entityManager;

public EntityManager getEntityManager() {
return entityManager;
}

public <T extends BaseEntity> T persistAndCommit(T t) {
entityManager.persist(t);
entityManager.refresh(t);
entityManager.getTransaction().commit();
return t;
}

...
...
}

所有测试的基类:

@RunWith(SpringJUnit4ClassRunner.class)
@Configurable(autowire = Autowire.BY_NAME)
@ContextConfiguration(locations = { "classpath:context-test.xml" })
public class BaseTest {
}

测试类:

public class CurrencyCreateTest extends BaseTest {

@Autowired
CRUDService crudService;

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void createCurrency() throws Exception {
Currency currency = new Currency();
currency.setCode("EUR");
currency.setName("Euro");
currency = crudService.persistAndCommit(currency);
}
}

上下文测试.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.chartinvest"/>

<bean id="contextApplicationContextProvider" class="com.chartinvest.util.ApplicationContextProvider"></bean>


<!-- the parent application context definition for the springapp application -->

<!-- dataSource -->
<bean id="dataSourceFinance" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>org.postgresql.Driver</value></property>
<property name="url"><value>jdbc:postgresql://localhost/db_finance_test</value></property>
<property name="username"><value>postgres</value></property>
<property name="password"><value>xxxxxxxx</value></property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="mypersistenceunit" />
<property name="dataSource" ref="dataSourceFinance" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
</map>
</property>
</bean>

<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

最佳答案

如果您的测试成功通过并且没有遇到任何异常,请在每个测试类中使用 @TransactionConfiguration(defaultRollback=false) 或在每个测试中使用 @Rollback(false)方法。事务测试将在 Spring 测试上下文中默认回滚。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"test-context.xml"})
@TransactionConfiguration(defaultRollback=false)
@Transactional
public class SampleCrudTest {

@Autowired
private SampleCrud sampleCrud;

@Before
public void onSetUpInTransaction() throws Exception {
//Populate Test Data
}


@Test
public void registerSample() {

Sample sample = new Sample("foo");
sampleCrud.save(sample);
assertNotNull(sample.getId());
}

}

关于java - 坚持/提交在 Spring JPA JUnit 的测试环境中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32922136/

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