Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring database for integration tests in micronaut

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

like image 278
Rajshree Rai Avatar asked Nov 06 '22 09:11

Rajshree Rai


1 Answers

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.

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

That test passes.

like image 119
Jeff Scott Brown Avatar answered Nov 14 '22 22:11

Jeff Scott Brown