i am trying to inject a mock to be used by a service class that i am testing but the mock does not seem to be used.
public class SpringDataJPARepo{ public void someMethod();// my repo }
i have a service class that i wish to test
@Service
public class Service implements IService{
@Autowired
private SpringDataJPARepo repository;
public String someMethod(){ // repository instance used here }
}
and i try to write my test cases by mocking the repository and injecting them into the service
@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
}
}
it throws a
Wanted but not invoked
in the verify method
i am not sure on how to inject the mock into the autowired instance. can anyone point out what i am doing wrong here?
Aucun commentaire:
Enregistrer un commentaire