問題描述
我在使用 @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-SNAPSHOT
和 2.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.
這篇關于Spring Boot 測試中的 MockBean 注解導致 NoUniqueBeanDefinitionException的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!