@InjectMocks @ Collaboration capability

Could you please help me 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 the test failed:
NP at the line "... workCircleServiceImpl.find () ...",

separately @InjectMocks and @Autowired work, but don't work together.

+3


source to share


1 answer


Usually when you are testing a device, you shouldn't initialize the Spring context. Therefore, remove Autowiring.

Usually when you do integration testing, you should be using real dependencies. So remove the taunt.



You are mixing integration and unit test here.

+7


source







All Articles