我想注入一个模拟被我正在测试的服务类使用,但模拟似乎没有被使用。弹簧注入模拟不使用
public class SpringDataJPARepo{ public void someMethod();// my repo }
我有希望测试
@Service
public class Service implements IService{
@Autowired
private SpringDataJPARepo repository;
public String someMethod(){ // repository instance used here }
}
服务类,我试图通过嘲笑库,并将其注入到服务
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={ServiceTestConfiguration.class})
public class Test{
@Mock
private SpringDataJPARepo repository;
@Autowired
@InjectMocks
private IService service;
@Before
public void setup(){
MockitoAnnotations.initMocks(this);
when(repository.someMethod()).thenReturn("test");
}
@Test
public testSomeMethod(){
assertThat(service.someMethod()).isNotNull;
verify(repository.someMethod,atLeast(1)); //fails here
}
}
它给我写测试用例抛出一个
通缉但未被调用
的验证方法
我不知道在如何注入模拟到自动装配实例。任何人都可以指出我在这里做错了什么?
是您的服务或为除了“@服务”任何Spring注解的方法,如“@交易“或”@Secured“? –
不,它没有任何其他注释 – austin
您的服务类除了'SpringDataJPARepo'外还有其他任何依赖吗? – Bunti