Quarkus PanacheEntity 在测试过程中未保存。

huangapple go评论90阅读模式
英文:

Quarkus PanacheEntity not saved during tests

问题

我正在尝试使用 Quarkus 和 Quarkus Testing 与 RestEasy 进行实验,但在每个测试之前设置虚拟数据时遇到了问题。

我正在尝试在调用端点之前使用 Active Record 模式插入一个用户。在调用 user.persistAndFlush()(或仅调用.persist())后,当向 User 实体询问用户计数时,它正确地返回数据库中有一条记录。然而,在通过 RestEasy 调用端点时,它对我抛出用户未找到的错误,并且确实,在记录服务中的某个方法调用时登出用户数量时,根据 Quarkus 的说法,有 0 个用户。
对我来说,好像带有@Test 注解的方法和服务本身在不同的上下文中运行?这有点奇怪,我确定有一些明显的遗漏。

这是我的测试类的样子:

@QuarkusTest
@QuarkusTestResource(PostgreSQLDatabaseResource.class)
public class AuthenticationResourceTest {

  @TestHTTPEndpoint(AuthenticationResource.class)
  @TestHTTPResource
  URL url;

  @BeforeEach
  @Transactional
  public void setUp() {
    User.deleteAll();
  }

  @Test
  public void testWithNonExistingEmail() {
    AuthenticationChallenge invalidAuthRequest = AuthenticationChallenge.builder()
        .email("admin@admin.com")
        .password("something")
        .build();

    given()
        .when()
        .contentType(ContentType.JSON)
        .body(invalidAuthRequest)
        .post(url)
        .then()
        .statusCode(401);
  }

  @Test
  @Transactional
  public void testWithValidAuthenticationChallenge() {
    User user = User.builder()
        .name("admin")
        .role(Roles.ADMIN)
        .status(Status.ACTIVE)
        .password(BCrypt.withDefaults().hashToString(10, "admin".toCharArray()))
        .email("admin@admin.com")
        .build();

    user.persistAndFlush();

    given()
        .when()
        .contentType(ContentType.JSON)
        .body(AuthenticationChallenge.builder().email("admin@admin.com").password("admin").build())
        .post(url)
        .then()
        .statusCode(200)
        .log()
        .all();
  }
}
英文:

I'm experimenting with Quarkus and Quarkus testing with RestEasy and I'm running into an issue while setting up dummy data before each of the tests.

I'm trying to insert a User using the Active Record pattern before calling the endpoint. Right after calling user.persistAndFlush() (or simply just calling .persist()) when the asking the User entity for the count of the users, it correctly gives back that there is one record in the database. However, when calling the endpoint through RestEasy, it throws back to me that the user is not found with that given identifier and indeed, when logging out the number of users in that certain method call in the service, there are 0 users according to Quarkus.
It looks to me as if the method with the @Test annotation and the service itself were running in a different context? It's a bit strange and I'm sure missing something obvious.

Here's what my test class looks like:

@QuarkusTest
@QuarkusTestResource(PostgreSQLDatabaseResource.class)
public class AuthenticationResourceTest {
@TestHTTPEndpoint(AuthenticationResource.class)
@TestHTTPResource
URL url;
@BeforeEach
@Transactional
public void setUp() {
User.deleteAll();
}
@Test
public void testWithNonExistingEmail() {
AuthenticationChallenge invalidAuthRequest = AuthenticationChallenge.builder()
.email("admin@admin.com")
.password("something")
.build();
given()
.when()
.contentType(ContentType.JSON)
.body(invalidAuthRequest)
.post(url)
.then()
.statusCode(401);
}
@Test
@Transactional
public void testWithValidAuthenticationChallenge() {
User user = User.builder()
.name("admin")
.role(Roles.ADMIN)
.status(Status.ACTIVE)
.password(BCrypt.withDefaults().hashToString(10, "admin".toCharArray()))
.email("admin@admin.com")
.build();
user.persistAndFlush();
given()
.when()
.contentType(ContentType.JSON)
.body(AuthenticationChallenge.builder().email("admin@admin.com").password("admin").build())
.post(url)
.then()
.statusCode(200)
.log()
.all();
}
}

答案1

得分: 2

你的用户信息直到测试方法结束时才会被保存到数据库,因为该方法是事务性的,事务会在最后提交。

通过你的 REST 调用,在事务外部查询了数据库,因此你不会得到数据。

如果你想要在 REST 调用中使用一些初始化数据,你需要在 @BeforeEach 方法中创建这些数据。

英文:

Your user won't be saved to the database until the end of the test method as it's transactional and the transaction is committed at the end.

With your REST call, you query your database outside of the transaction so you won't have the data.

If you want to initialize some data visible to your REST call, you need to create the data in the @BeforeEach method.

huangapple
  • 本文由 发表于 2020年8月30日 21:29:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63657946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定