我尝试为我的JSF应用程序实现一些测试,并使用mockito进行模拟。 (我也使用spring)
@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
方法检索实例时,我之前通过构造函数设置的所有参数都为null。我对模拟对象很陌生,请问这种行为是正常的,还是我的代码有问题?