【发布时间】:2020-10-14 15:20:04
【问题描述】:
我有一个 Spring MvC 应用程序。 (Spring Framework是Java平台的应用框架和反转控制容器)用这个测试:
@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));
}
...
}
但是当我运行测试时,似乎没有模拟 repo
【问题讨论】:
-
您使用的是哪个junit。 Junit4 还是 Junit5?你能用导入显示代码吗?
-
我使用的是 4.13
-
您正在模拟最终对象。模拟最终对象通常会返回 null。您可以尝试将其从 final 更改。
标签: java spring spring-mvc junit mockito