3
你能幫助我,請 一些代碼:@InjectMocks @Autowired一起發出
@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()...
}
但測試失敗:
NP在 「... workCircleServiceImpl.find()......」 行,
@InjectMocks & @Autowired工作,但一起工作。
我認爲你應該只使用'@InjectMocks'和'@ Autowired'之一... – Xstian 2014-11-05 13:22:55