Spring Boot 测试中的 MockBean 注解导致 NoUniqueBeanDefinitionException

我在使用 @MockBean 注释时遇到问题.文档说 MockBean 可以替换上下文中的 bean,但我在单元测试中得到了 NoUniqueBeanDefinitionException.我看不到如何使用注释.如果我可以模拟 repo,那么显然会有不止一个 b​​ean 定义.

I am having trouble using the @MockBean annotation. The docs say MockBean can replace a bean within the context, but I am getting a NoUniqueBeanDefinitionException within my unit test. I can't see how to use the annotation. If I can mock the repo, then obviously there will be more than one bean definition.

我正在关注此处的示例:https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4

I am following the examples found here: https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4

我有一个 mongo 存储库:

I have a mongo repository:

public interface MyMongoRepository extends MongoRepository<MyDTO, String>
{
   MyDTO findById(String id);
}

还有泽西岛资源:

@Component
@Path("/createMatch")
public class Create
{
    @Context
    UriInfo uriInfo;

    @Autowired
    private MyMongoRepository repository;

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Response createMatch(@Context HttpServletResponse response)
    {
        MyDTO match = new MyDTO();
        match = repository.save(match);
        URI matchUri = uriInfo.getBaseUriBuilder().path(String.format("/%s/details", match.getId())).build();

        return Response.created(matchUri)
                .entity(new MyResponseEntity(Response.Status.CREATED, match, "Match created: " + matchUri))
                .build();
    }
}

还有一个 JUnit 测试:

And a JUnit test:

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestMocks {

    @Autowired
    private TestRestTemplate restTemplate;

    @MockBean
    private MyMongoRepository mockRepo;

    @Before
    public void setup()
    {
        MockitoAnnotations.initMocks(this);

        given(this.mockRepo.findById("1234")).willReturn(
                new MyDTO());
    }

    @Test
    public void test()
    {
        this.restTemplate.getForEntity("/1234/details", MyResponseEntity.class);

    }

}

错误信息:

Field repository in path.to.my.resources.Create required a single bean, but 2 were found:
    - myMongoRepository: defined in null
    - path.to.my.MyMongoRepository#0: defined by method 'createMock' in null

推荐答案

这是一个错误:https://github.com/spring-projects/spring-boot/issues/6541

修复在 spring-data 1.0.2-SNAPSHOT2.0.3-SNAPSHOT 中:https://github.com/arangodb/spring-data/issues/14#issuecomment-374141173

The fix is in spring-data 1.0.2-SNAPSHOT and 2.0.3-SNAPSHOT : https://github.com/arangodb/spring-data/issues/14#issuecomment-374141173

如果您不使用这些版本,您可以通过使用其名称声明模拟来解决它:

If you aren't using these version, you can work around it by declaring the mock with its name:

@MockBean(name="myMongoRepository")
private MyMongoRepository repository;

<小时>

回应您的评论

来自 Spring 的文档:

为方便起见,需要对开始的 REST 调用的测试服务器还可以 @Autowire 一个 TestRestTemplate 这将解析到正在运行的服务器的相对链接.

For convenience, tests that need to make REST calls to the started server can additionally @Autowire a TestRestTemplate which will resolve relative links to the running server.

读到这里,我觉得你需要用web环境声明@SpringBootTest:

Reading this, I think you need to declare @SpringBootTest with a web environment:

@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)

如果你的spring boot没有启动web环境,那还需要什么TestRestTemplate.因此,我猜 spring 甚至没有提供它.

If your spring boot doesn't start the web environment, then what is the need for TestRestTemplate. Thus, I guess spring does not even make it available.

相关文章