gpt4 book ai didi

java - 在 micronaut 中为集成测试配置数据库

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

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

我有一个 Controller 类:

@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> {}

最佳答案

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

那个测试通过了。

关于java - 在 micronaut 中为集成测试配置数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60149098/

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