gpt4 book ai didi

spring - DataJpaTest 无法在单元测试中更新

转载 作者:行者123 更新时间:2023-12-03 19:50:21 24 4
gpt4 key购买 nike

我试图找出为什么我的单元测试不起作用。我看到 Hibernate 在更新之前做了一个插入。
为什么这样做?
是测试失败的原因吗?

我已经为测试环境设置了 hsqldb,该服务在 mysql 中似乎运行良好。

@Repository
public interface UserDataRepository extends CrudRepository<UserData, Integer> {

@Transactional
@Modifying
@Query("UPDATE UserData ud SET chips = chips + :delta WHERE ud.id = :userId")
void addChipsToUser(@Param("userId") int userId, @Param("delta") long delta);
}

我的测试类:
@RunWith(SpringRunner.class)
@DataJpaTest
public class TestJPASlice {

@Autowired
private TestEntityManager entityManager;

@Autowired
private UserDataRepository repository;

@Test
public void testAddChipsToUser() {
UserData data = new UserData();
data.setChips(100);
data.setId(13);
this.entityManager.persist(data);

System.err.println("pre u");
this.repository.addChipsToUser(13, 500);
System.err.println("post u");

UserData two = this.repository.findOne(13);
assertThat(two.getId()).isEqualTo(13);
assertThat(two.getChips()).isEqualTo(600);
}

这是我得到的输出:
Hibernate: update user_player_data set chips=chips+? where user_id=?
2017-08-10 11:33:37.794 WARN 2128 --- [main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning Code: -1100, SQLState: 02000
2017-08-10 11:33:37.795 WARN 2128 --- [main] o.h.engine.jdbc.spi.SqlExceptionHelper : no hay datos
2017-08-10 11:33:37.796 INFO 2128 --- [main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test context [DefaultTestContext@29626d54...
2017-08-10 11:33:37.801 INFO 2128 --- [main] o.s.t.c.transaction.TransactionContext : Began transaction (1) for test context [DefaultTestContext@29626d54...
pre u
Hibernate: insert into user_player_data (chips, user_id) values (?, ?)
Hibernate: update user_player_data set chips=chips+? where user_id=?
post u
2017-08-10 11:33:37.874 INFO 2128 --- [main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test context [DefaultTestContext@29626d54...
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 3.073 sec <<< FAILURE! - in service.chipBank.TestJPASlice
testAddChipsToUser(service.chipBank.TestJPASlice) Time elapsed: 0.074 sec <<< FAILURE!
org.junit.ComparisonFailure: expected:<[6]00L> but was:<[1]00L>
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at service.chipBank.TestJPASlice.testAddChipsToUser(TestJPASlice.java:43)

最佳答案

要使其工作,您所要做的就是启用 clearAutomatically 标志:

@Modifying(clearAutomatically = true)

以便在执行修改查询后清除底层持久化上下文。

然后,您可以在日志中看到后续的选择查询:
2017-08-10 22:20:09.693 DEBUG 1948 --- [           main] org.hibernate.SQL                        : insert into user_player_data (chips, user_id) values (?, ?)
Hibernate: insert into user_player_data (chips, user_id) values (?, ?)
2017-08-10 22:20:09.695 TRACE 1948 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [100]
2017-08-10 22:20:09.696 TRACE 1948 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [INTEGER] - [13]
2017-08-10 22:20:09.700 DEBUG 1948 --- [ main] org.hibernate.SQL : update user_player_data set chips=chips+? where user_id=?
Hibernate: update user_player_data set chips=chips+? where user_id=?
2017-08-10 22:20:09.700 TRACE 1948 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [500]
2017-08-10 22:20:09.701 TRACE 1948 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [INTEGER] - [13]
post u
2017-08-10 22:20:09.708 DEBUG 1948 --- [ main] org.hibernate.SQL : select userdata0_.user_id as user_id1_0_0_, userdata0_.chips as chips2_0_0_ from user_player_data userdata0_ where userdata0_.user_id=?
Hibernate: select userdata0_.user_id as user_id1_0_0_, userdata0_.chips as chips2_0_0_ from user_player_data userdata0_ where userdata0_.user_id=?
2017-08-10 22:20:09.708 TRACE 1948 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [13]
2017-08-10 22:20:09.712 TRACE 1948 --- [ main] o.h.type.descriptor.sql.BasicExtractor : extracted value ([chips2_0_0_] : [BIGINT]) - [600]
2017-08-10 22:20:09.751 INFO 1948 --- [ main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test context [DefaultTestContext@ca263c2 testClass = TestJPASlice, testInstance = com.example.demo.TestJPASlice@2145433b, testMethod = testAddChipsToUser@TestJPASlice, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@589b3632 testClass = TestJPASlice, locations = '{}', classes = '{class com.example.demo.DemoApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[[ImportsContextCustomizer@45f45fa1 key = [org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@7a765367, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@3043fe0e, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@c46bcd4, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@ee27f40e, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@cb51256], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]].
2017-08-10 22:20:09.753 INFO 1948 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@4944252c: startup date [Thu Aug 10 22:20:07 BST 2017]; root of context hierarchy
2017-08-10 22:20:09.754 INFO 1948 --- [ Thread-2] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2017-08-10 22:20:09.754 INFO 1948 --- [ Thread-2] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
2017-08-10 22:20:09.755 DEBUG 1948 --- [ Thread-2] org.hibernate.SQL : drop table user_player_data if exists
Hibernate: drop table user_player_data if exists

有关标志 take a look at the Spring Data JPA Documentation 的更多信息。

关于spring - DataJpaTest 无法在单元测试中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45617040/

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