could you help me please, some code:
@ContextConfiguration(locations = { "/applicationContext.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class TestUnit2 {
@Mock
private MongoOperations mongoTemplate;
@InjectMocks
@Autowired
private WorkcircleRepositoryMongoImpl workCircleRepository;
@Autowired
private WorkcircleServiceImpl workCircleServiceImpl;
@Before
public void setUp() {
....
when(mongoTemplate.findOne(new Query(), Person.class)).thenReturn(expectedPerson);
MockitoAnnotations.initMocks(this);
}
@Test
public void test() {
... workCircleServiceImpl.find()...
}
But test is failed:
NP in "... workCircleServiceImpl.find()..." line,
in separate way @InjectMocks & @Autowired work, but together are not worked.
Usually when you are unit testing, you shouldn't initialize Spring context. So remove Autowiring.
Usually when you do integration testing, you should use real dependencies. So remove mocking.
You are mixing integration and unit test here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With