5

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

我有一个控制器类:

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

有没有办法解决这个问题?

编辑:这是我的存储库类

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

4

1 回答 1

1

有没有办法解决这个问题?

是的。

如何做到这一点的细节将取决于更多地了解您的项目。您没有显示足够的信息来知道出了什么问题,所以我将您的代码粘贴到一个项目中,该项目表明您问题中的代码似乎有效。唯一不清楚的是你的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

那个测试通过了。

于 2020-02-11T02:34:51.117 回答