为 micronaut 中的集成测试配置数据库

Configuring database for integration tests in micronaut

我正在尝试在 micronaut 中编写集成测试。

我有一个控制器class:

@Controller("/hello")
public class HelloController {

    @Inject
    private HelloRepository helloRepository;

    @Get("/")
    public HttpResponse get() {
        return HttpResponse.ok(helloRepository.findAll());
    }
}

我正在尝试为其编写集成测试,例如:

@MicronautTest
public class HelloControllerSpec {

    @Inject
    EmbeddedServer embeddedServer;

   @BeforeEach
    void setUp() {
        initMocks(this);
    }

    @Test
    public void testIndex() throws Exception {
        try(RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL())) {
           client.toBlocking().exchange("/hello").status();
        }
    }
}

但我一直收到错误消息:

 No backing RepositoryOperations configured for repository. Check your configuration and try again  

我放在 "src/test/java/resources/" 下的 application.yml 文件具有以下数据源实现:

datasources:
   default:
      url: jdbc:h2:mem:devDb
      driverClassName: org.h2.Driver
      username: sa
      password: 'sa'
      schema-generate: CREATE_DROP
      dialect: H2
jpa:
   default:
      packages-to-scan:
         - 'com.myproject.project'
      properties:
         hibernate:
            hbm2ddl:
               auto: update
            show_sql: true

我也将其包含在我的 build.gradle 文件中

runtime 'com.h2database:h2'

有什么办法可以解决吗?

编辑:这是我的存储库class

@Repository
public interface HelloRepository extends CrudRepository<BufferConditionEntity, Long> {}

Is there any way to solve this?

是的。

如何执行此操作的细节取决于对您的项目的更多了解。 您没有显示足够的信息来知道问题出在哪里,所以我将您的代码粘贴到一个项目中,该项目显示您问题中的代码似乎有效。唯一不清楚的是您的 initMocks 方法的作用。

https://github.com/jeffbrown/rajshreerairepository 查看项目。

https://github.com/jeffbrown/rajshreerairepository/blob/4abe71d7a4ac9ae3cbcdb164f4d2c58249e29106/src/main/java/com/myproject/project/HelloController.java

package com.myproject.project;

import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

import javax.inject.Inject;

@Controller("/hello")
public class HelloController {

    @Inject
    private HelloRepository helloRepository;

    @Get("/")
    public HttpResponse get() {
        return HttpResponse.ok(helloRepository.findAll());
    }
}

https://github.com/jeffbrown/rajshreerairepository/blob/4abe71d7a4ac9ae3cbcdb164f4d2c58249e29106/src/test/java/com/myproject/project/HelloControllerTest.java

package com.myproject.project;

import io.micronaut.http.HttpStatus;
import io.micronaut.http.client.RxHttpClient;
import io.micronaut.runtime.server.EmbeddedServer;
import io.micronaut.test.annotation.MicronautTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;

import static org.junit.jupiter.api.Assertions.assertEquals;

@MicronautTest
public class HelloControllerTest {
    @Inject
    EmbeddedServer embeddedServer;

    @BeforeEach
    void setUp() {
        initMocks(this);
    }

    @Test
    public void testIndex() throws Exception {
        try (RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL())) {
            assertEquals(HttpStatus.OK, client.toBlocking().exchange("/hello").status());
        }
    }

    void initMocks(Object o) {
        // unclear if this method is relevant
    }
}

https://github.com/jeffbrown/rajshreerairepository/blob/87898caad4699436c60c84b85058d29885e7ec9f/src/main/java/com/myproject/project/HelloRepository.java

package com.myproject.project;

import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;

@Repository
public interface HelloRepository extends CrudRepository<BufferConditionEntity, Long> {
}

https://github.com/jeffbrown/rajshreerairepository/blob/87898caad4699436c60c84b85058d29885e7ec9f/src/main/resources/application.yml

---
micronaut:
  application:
    name: rajshreerairepository
---
datasources:
  default:
    url: jdbc:h2:mem:devDb
    driverClassName: org.h2.Driver
    username: sa
    password: 'sa'
    schema-generate: CREATE_DROP
    dialect: H2
jpa:
  default:
    packages-to-scan:
      - 'com.myproject.project'
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        show_sql: true

测试通过。