Mockito 模拟对象返回 null
我尝试为我的JSF应用程序和我使用的模拟实现一些测试。(也用弹簧)
@RunWith(MockitoJUnitRunner.class)
public class GeneralConfigServiceImplTest {
private GeneralConfigService generalConfigService;
@Mock
private GeneralConfigDAO generalConfigDAO;
@Mock
private GeneralConfig gen;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
generalConfigService = new GeneralConfigService();
ReflectionTestUtils.setField(generalConfigService, "generalConfigDAO", generalConfigDAO);
}
@Test
public void testAddGeneralConfigCallDAOSuccess() throws DAOException, EntityNullException, IllegalEntityArgumentException, ParseException, EntityPersistException {
gen = createGeneralConfigs("label", "value");
generalConfigService.setInstance(gen);
generalConfigService.persist();
log.info(generalConfigService.getInstance().toString());
}
}
测试成功,但当我想使用该方法检索实例时。我之前设置的所有参数(通过之前的构造函数)都是空的。我是模拟对象的新手,所以这种行为是正常的,还是我的代码中有错误?getInstance