问题描述
我正在尝试在我的 Java Junit 测试方法中使用 mockit.Expectation。但它没有表现出预期的行为。
我能够调试测试方法,并且在 YoucalcModelVO.java 的 lBeanList 中得到 NULL。
被测类:
import static org.testng.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.dashboard.service.GetDashLabel;
import com.i18n.bean.LocalLabelBean;
import com.sca.ServiceApplicationException;
import com.sca.ServiceCommandHandler;
import com.sca.service.ServiceCommandHandlerFactory;
import com.bean.preference.PreferenceBean;
import com.service.preference.GetPreferenceBean;
import com.appletsheet.AppSignature;
import com.appletsheet.TDocument;
import com.appletsheet.AppSignature.WidgetInfo.ReportInfo;
import com.dao.PersistenceAPI;
import com.service.AppAdminService;
import com.service.AppService;
import com.bean.Application;
import mockit.Deencapsulation;
import mockit.Expectations;
import mockit.Injectable;
import mockit.Mocked;
public class CommonControllerTest {
CommonController commCont;
@Injectable
PersistenceAPI persistenceAPI;
@Mocked
Application app;
@Mocked()
private ServiceCommandHandler mockServiceCommandHandler;
@Test(groups = { "checkin" })
public void getAppForEditForNonEmptyDocumentKeytest() throws Exception{
commCont= new CommonController();
Deencapsulation.setField(commCont,"PersistenceAPI",persistenceAPI);
Deencapsulation.setField(commCont,"AppAdminService",appAdminService);
Deencapsulation.setField(commCont,"AppService",appService);
List<LocalLabelBean> localLabellist=new ArrayList<LocalLabelBean>();
LocalLabelBean lb=new LocalLabelBean();
lb.setLabel("testLabel");
lb.setLocale(new Locale("ko"));
localLabellist.add(lb);
new Expectations(ServiceCommandHandlerFactory.class) {
{
persistenceAPI.getApplicationById(anyLong,anyBoolean);
result = app;
ServiceCommandHandlerFactory.getSCAHandler();
result = mockServiceCommandHandler;
mockServiceCommandHandler.execute(withInstanceOf(GetDashLabel.class));
result = localLabellist;
}
};
assertNotNull(commCont.getAppForEdit(24));
}
CommonController.java:
public AjaxServiceStatus getAppForEdit(long appId) {
AjaxServiceStatus serviceStatus = new AjaxServiceStatus();
Application app;
app = PersistenceAPI.getApplicationById(appId,false);
if (app == null) {
serviceStatus.setSTatusMsg(msgc.getString("ADMIN"));
}
else{
YoucalcModelVO youcalcModelVO = new YoucalcModelVO(app);
youcalcModelVO.localizeNames(); **// Here is the problem....**
}
}
YoucalcModelVO.java:
public void localizeNames(){
LocalLabelBean locBean = new LocalLabelBean();
List<LocalLabelBean> lBeanList = new ArrayList<>();
GetDashLabel cmd = new GetDashLabel();
cmd.setLocalLabelBean(locBean);
try {
ServiceCommandHandler scaHandler = ServiceCommandHandlerFactory.getSCAHandler();
lBeanList = scaHandler.execute(cmd); **// lBeanList becomes Null**
}
for (LocalLabelBean lBean : lBeanList) { **// NULLPointerException thrown here**
}
}
我想了解为什么 @Test 方法中 new Expectations(ServiceCommandHandlerFactory.class) 中的代码没有按预期工作。
对于行“ServiceCommandHandler scaHandler = ServiceCommandHandlerFactory.getSCAHandler();”在 YoucalcModelVO.java 中,行 ServiceCommandHandlerFactory.getSCAHandler(); 结果 = mockServiceCommandHandler;内部@Test 方法应该被执行。
对于“lBeanList = scaHandler.execute(cmd);”这一行在 YoucalcModelVO.java 中,行 "mockServiceCommandHandler.execute(withInstanceOf(GetDashLabel.class)); result = localLabellist;" 应该在@Test 方法中执行。
但它没有按预期工作,我在调试时在 lBeanList 中得到 null。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)