今天写单元测试用例,跑起来后,出现了空指针异常。于是查了下,发现Mock对象的一个属性未注入,为null。我的程序结构大致为:
@Repository
public class MyRepository {
public void doSomething() {
System.out.println("here's dosomething");
}
public Model findById(Long id) {
return new Model(id, "Real Repository");
}
}
@Service
public class MyService {
@Autowired
private MyRepository myRepository;
public void doSomething() {
this.myRepository.doSomething();
}
public Model findById(Long id) {
return this.myRepository.findById(id);
}
}
@Controller
public class MyController {
@Autowired
private MyService myService;
public void doSomething() {
this.myService.doSomething();
}
public Model findById(Long id) {
return this.myService.findById(id);
}
}
@RunWith(MockitoJUnitRunner.class)
public class MyControllerTest {
@Mock
private MyRepository myRepository;
@InjectMocks
private MyService myService;
@InjectMocks
private MyController myController;
@Before
public void setUp() throws Exception {
Model model = new Model(11L, "AAA");
doNothing().when(myRepository).doSomething();
when(myRepository.findById(11L)).thenReturn(model);
}
@Test
public void doSomething() throws Exception {
this.myController.doSomething();
}
@Test
public void findById() throws Exception {
System.out.println(this.myController.findById(11L));
}
}
org.mockito.exceptions.base.MockitoException: This combination of annotations is not permitted on a single field:
@Mock and @InjectMocks
于是只能另寻他路,考虑到使用Spring来做容器管理,修改Test类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:beans.xml"})
public class MyControllerTest {
@Mock
private MyRepository myRepository;
@InjectMocks
@Autowired
private MyService myService;
@Autowired
private MyController myController;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
Model model = new Model(11L, "AAA");
doNothing().when(myRepository).doSomething();
when(myRepository.findById(11L)).thenReturn(model);
}
@Test
public void doSomething() throws Exception {
this.myController.doSomething();
}
@Test
public void findById() throws Exception {
System.out.println(this.myController.findById(11L));
}
}
果然可以了,模拟了数据库访问:
@autowire加哪里还有讲究,必须2个都加,否则也会出现注入不到的情况。
既然没法注入,其实不借助容器,也可以手动来赋值。在setup方法中做下修改,便得到方法二:
@RunWith(MockitoJUnitRunner.class)
public class MyControllerTest {
@Mock
private MyRepository myRepository;
@InjectMocks
private MyService myService;
@InjectMocks
private MyController myController;
@Before
public void setUp() throws Exception {
ReflectionTestUtils.setField(myController, "myService", myService);
Model model = new Model(11L, "AAA");
doNothing().when(myRepository).doSomething();
when(myRepository.findById(11L)).thenReturn(model);
}
@Test
public void doSomething() throws Exception {
this.myController.doSomething();
}
@Test
public void findById() throws Exception {
System.out.println(this.myController.findById(11L));
}
}
两种方法都能满足多级的注入情形。如果不想依赖Spring,可以选择用第2种的方式。