2

我尝试为我的 API 控制器编写 UnitTest,它使用来自 micronaut-data-jpa 的 CrudRepository。我使用@MockBean 在我的控制器中模拟协作存储库。如果我运行测试,我会收到以下错误:

Message: No such method [findById(java.lang.Object) ] for bean [jens.repositories.ConnectionProfileRepository]

我从未使用过 Spock、Micronaut-Data 或 Micronaut-Test,仅从以下来源尝试过:

https://micronaut-projects.github.io/micronaut-test/latest/guide/index.html

控制器:

ConnectionProfileRepository profileRepository

    ProfilesController(ConnectionProfileRepository profileRepository) {
        this.profileRepository = profileRepository
    }

语言:Groovy 存储库

interface ConnectionProfileRepository extends CrudRepository<ConnectionProfile, UUID> { }

测试

@MicronautTest
class ProfilesControllerSpec extends Specification {

    @Inject
    ConnectionProfileRepository profilesRepositoryMock

    @MockBean(ConnectionProfileRepository.class)
    ConnectionProfileRepository profileRepository() {
        Mock(ConnectionProfileRepository)
    }
...

如何使用当前设置进行管理以实现控制器中存储库的模拟?

4

2 回答 2

4

请参阅https://github.com/jeffbrown/mockmdrepository上的项目。

https://github.com/jeffbrown/mockmdrepository/blob/fe7ce34e526663455f6b4322fc38bed2ddcf11dc/src/main/java/mockmdrepository/ProfileRepository.java

package mockmdrepository;

import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.repository.CrudRepository;

@JdbcRepository(dialect = Dialect.H2)
public interface ProfileRepository extends CrudRepository<Profile, Long> { }

https://github.com/jeffbrown/mockmdrepository/blob/fe7ce34e526663455f6b4322fc38bed2ddcf11dc/src/main/java/mockmdrepository/ProfilesController.java

package mockmdrepository;

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

import java.util.Optional;

@Controller("/profiles")
public class ProfilesController {

    private final ProfileRepository profileRepository;

    public ProfilesController(ProfileRepository profileRepository) {
        this.profileRepository = profileRepository;
    }

    @Get("/")
    public Iterable<Profile> index() {
        return profileRepository.findAll();
    }

    @Get("/{id}")
    public Optional<Profile> get(Long id) {
        return profileRepository.findById(id);
    }
}

https://github.com/jeffbrown/mockmdrepository/blob/fe7ce34e526663455f6b4322fc38bed2ddcf11dc/src/test/groovy/mockmdrepository/ProfilesControllerSpec.groovy

package mockmdrepository

import io.micronaut.context.annotation.Replaces
import io.micronaut.http.HttpResponse
import io.micronaut.http.client.RxHttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.annotation.MicronautTest
import io.micronaut.test.annotation.MockBean
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification

import javax.inject.Inject

@MicronautTest
class ProfilesControllerSpec extends Specification {

    @Shared @AutoCleanup @Inject @Client("/")
    RxHttpClient client

    @Inject
    ProfileRepository repository

    void "test index"() {
        when:
        1 * repository.findAll() >> [new Profile('Profile 1'), new Profile('Profile 2')]
        HttpResponse<Iterable<Profile>> response = client.toBlocking().exchange("/profiles", Iterable)

        then:
        response.body().size() == 2
    }

    void "test get"() {
        when:
        1 * repository.findById(42) >> Optional.of(new Profile('Profile Forty Two'))
        HttpResponse<Iterable<Profile>> response = client.toBlocking().exchange("/profiles/42", Profile)

        then:
        response.body().name == 'Profile Forty Two'
    }

    @MockBean
    @Replaces(ProfileRepository)
    ProfileRepository mockRepo() {
        Mock(ProfileRepository)
    }
}
于 2019-12-23T18:38:35.050 回答
-1

我认为您可能需要通过控制器中的实例成员而不是构造函数注入来注入 crud 存储库。至少它对我有用。我的是java代码,groovy应该是一样的。

在控制器中:

   @Inject
    ParameterRepository parameterRepository;

在测试中:

    @MockBean(ParameterRepository.class)
    ParameterRepository parameterRepository() {
        return mock(ParameterRepository.class);
    }

    @Inject
    ParameterRepository parameterRepository;
于 2019-10-04T06:14:29.150 回答