Reputation: 642
I am trying to write an integration test in micronaut.
I have a controller class:
@Controller("/hello")
public class HelloController {
@Inject
private HelloRepository helloRepository;
@Get("/")
public HttpResponse get() {
return HttpResponse.ok(helloRepository.findAll());
}
}
I am trying to write an integration test for it like:
@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();
}
}
}
But I keep getting the error:
No backing RepositoryOperations configured for repository. Check your configuration and try again
My application.yml file that I put under "src/test/java/resources/" has the following datasource implementation:
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
I have also included this in my build.gradle file
runtime 'com.h2database:h2'
Is there any way to solve this?
Edit: This is my repository class
@Repository
public interface HelloRepository extends CrudRepository<BufferConditionEntity, Long> {}
Upvotes: 5
Views: 4836
Reputation: 27245
Is there any way to solve this?
Yes.
The particulars of how to do that will depend on knowing more about your project.
You haven't shown enough information to know what is wrong so I pasted your code into a project which shows that the code in your question appears to work. The only thing that isn't clear is what your initMocks
method does.
See the project at https://github.com/jeffbrown/rajshreerairepository.
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());
}
}
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
}
}
package com.myproject.project;
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;
@Repository
public interface HelloRepository extends CrudRepository<BufferConditionEntity, Long> {
}
---
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
That test passes.
Upvotes: 1