[英]Mockito Bug in Spring 2.0.0.RELEASE
我用 Spring 的最新版本 (2.0.0.RELEASE) 更新了我的項目,雖然我的測試在 2.0.0.RC1 中工作,但現在它不起作用,它一直給我這個錯誤:
org.mockito.exceptions.base.MockitoException:
Cannot instantiate @InjectMocks field named 'service'! Cause: the type 'PersonService' is an interface.
You haven't provided the instance at field declaration so I tried to construct the instance.
Examples of correct usage of @InjectMocks:
@InjectMocks Service service = new Service();
@InjectMocks Service service;
//and... don't forget about some @Mocks for injection :)
在這里,我做了一個最小的項目,您可以在其中更改 pom 文件中的版本,以查看它在 2.0.0.RC1 上成功並在 2.0.0.RELEASE 中失敗。
對於完整的最小測試 - 請轉到gituhub 。
來自@InjectMocks
的文檔
Mockito 不能實例化內部類、本地類、抽象類,當然還有接口。
在您的情況下,您應該在測試中使用:
@InjectMocks
private PersonServiceImpl underTest;
我已經從 github 簽入了你的示例,如果你更改為服務的實現 - 測試將通過
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.