Java Spring框架-Mock不是Mock
我有一个Spring MvC应用程序。(Spring框架是Java平台的应用程序框架和控制反转容器)通过以下测试:Java Spring框架-Mock不是Mock,java,spring,spring-mvc,junit,mockito,Java,Spring,Spring Mvc,Junit,Mockito,我有一个Spring MvC应用程序。(Spring框架是Java平台的应用程序框架和控制反转容器)通过以下测试: @RunWith(SpringJUnit4ClassRunner.class) public class AutorisationServiceTest { @Autowired @InjectMocks private IAutorisationService service; @
@RunWith(SpringJUnit4ClassRunner.class)
public class AutorisationServiceTest {
@Autowired
@InjectMocks
private IAutorisationService service;
@Mock
PersonneRepository personneRepository = Mockito.mock(PersonneRepository.class);
@Before
public void setup() {
}
@Test
public void should_Find_GardeWith2Affectations() throws IOException {
when(personneRepository.getAll(anyString())).thenReturn(DataLoader.mockData());
service.getAll("rules");
}
}
@Service
public class AutorisationService implements IAutorisationService {
private final PersonneRepository personneRepository;
public AutorisationService(PersonneRepository personneRepository) {
this.personneRepository = personneRepository;
}
@Override
public List<User> getAll(String where) {
return personneRepository.getAll(where));
}
...
}
@RunWith(SpringJUnit4ClassRunner.class)
公共类自动恢复服务测试{
@自动连线
@注射模拟
私人智能交通服务;
@嘲弄
PersonRepository personRepository=Mockito.mock(personRepository.class);
@以前
公共作废设置(){
}
@试验
public void应该发现带有2Effections()的异常{
when(personneRepository.getAll(anyString()).thenReturn(DataLoader.mockData());
服务。getAll(“规则”);
}
}
@服务
公共类AutorisationService实现IAutorisationService{
私人最终人员存储库人员存储库;
公共自动恢复服务(人员存储库人员存储库){
this.PersonRepository=PersonRepository;
}
@凌驾
公共列表getAll(字符串,其中){
返回personneRepository.getAll(where));
}
...
}
但是,当我运行测试时,模拟repo似乎是不可能的,您不能同时使用
@mock
和Mockito.mock
。在@Before安装方法中选择其中一个,您需要添加MockitoAnnotations.initMocks(此)。这将注入模拟服务。我已修改了以下代码:
@RunWith(SpringJUnit4ClassRunner.class)
public class AutorisationServiceTest {
@Mock
PersonneRepository personneRepository;
@InjectMocks
private AutorisationService service;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void should_Find_GardeWith2Affectations() throws IOException {
when(personneRepository.getAll(anyString())).thenReturn(DataLoader.mockData());
service.getAll("rules");
}
}
@Service
public class AutorisationService implements IAutorisationService {
private final PersonneRepository personneRepository;
public AutorisationService(PersonneRepository personneRepository) {
this.personneRepository = personneRepository;
}
@Override
public List<User> getAll(String where) {
return personneRepository.getAll(where));
}
...
}
@RunWith(SpringJUnit4ClassRunner.class)
公共类自动恢复服务测试{
@嘲弄
人员库人员库;
@注射模拟
私人自助服务;
@以前
公共作废设置(){
initMocks(this);
}
@试验
public void应该发现带有2Effections()的异常{
when(personneRepository.getAll(anyString()).thenReturn(DataLoader.mockData());
服务。getAll(“规则”);
}
}
@服务
公共类AutorisationService实现IAutorisationService{
私人最终人员存储库人员存储库;
公共自动恢复服务(人员存储库人员存储库){
this.PersonRepository=PersonRepository;
}
@凌驾
公共列表getAll(字符串,其中){
返回personneRepository.getAll(where));
}
...
}
此答案适用于测试NG:
MockitoTestExecutionListener
初始化mock,您需要使用@MockBean
注释
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners(MockitoTestExecutionListener.class)
public class AutorisationServiceTest {
@MockBean
PersonneRepository personneRepository;
另外,不要忘记添加
resetmockstexecutionListener
以防止测试间的持久性模拟(参见此)。您应该决定是编写Spring测试还是Mockito测试
spring测试将加载完整的上下文,然后您可以使用@Autowired和@MockBean:
@RunWith(SpringJUnit4ClassRunner.class)
公共类自动恢复服务测试{
@自动连线
私人智能交通服务;
@蚕豆
人员库人员库;
Mockito测试运行得快得多,但您将只拥有显式配置为@injectmock或@Mock的对象:
@RunWith(MockitoJUnitRunner.class)
公共类自动恢复服务测试{
@注射模拟
私人智能交通服务;
@嘲弄
人员库人员库;
请注意,在使用注释和专门的运行程序时,您永远不必将mock与Mockito.mock()实例化
我猜您的PersonRepository是一个@Service、@Component或@Repository。因此您也应该在此处使用自动连接:
@服务
公共类AutorisationService实现IAutorisationService{
@自动连线
个人知识库;
您使用的是哪个junit。Junit4还是Junit5?可以用导入显示代码吗?我使用的是4.13。您正在模拟最终对象。模拟最终对象通常会返回null。您可以尝试将其从最终对象更改为null。我删除了Mockito.mock(PersonneRepository.class),但是我得到了一个空指针。你能在没有Autowire注释的情况下启动服务吗?