我正在尝试为seam中的以下方法编写单元测试。 要做到这一点......我需要模拟facesContext和UIComponent并将其传递给方法getAsObject。
我尝试使用Jmock和接缝但遇到问题。有什么建议吗?
public Object getAsObject(javax.faces.context.FacesContext facesContext, UIComponent uiComponent, String s) throws ConverterException
{
WorkcaseFilterCache workcaseFilterCache = (WorkcaseFilterCache) Component.getInstance("workcaseFilterCache");
ValueBinding binding = uiComponent.getValueBinding("value");
Class filterType = binding.getType(facesContext);
Object returnObject = null;
if (s.equals(NO_SELECTION_VALUE)) {
return null;
}
if (filterType.isAssignableFrom(WorkcaseType.class)) {
if (s == null || s.equals("null")) {
return null;
} else {
try {
Long workcaseTypeId = Long.parseLong(s);
Object value = workcaseFilterCache.getWorkcasesTypeMap().get(workcaseTypeId);
if (value != null) {
returnObject = value;
}
} catch (Exception e) {
logger.error(e.toString());
}
}
}
}
使用jMock时遇到的问题。
public Mockery mockeryContext = new JUnit4Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
FacesContext mockfacesContext1 = this.mockeryContext.mock(FacesContext.class);
UIComponent mockUiComponent1 = this.mockeryContext.mock(UIComponent.class);
Application mockApplication1 = this.mockeryContext.mock(Application.class);
ValueBinding vb = mockfacesContext1.getApplication().createValueBinding("WorkcaseType.class");
mockfacesContext1.getApplication().createValueBinding("WorkcaseType.class"); ' gives assertion error
我尝试使用.. org.jboss.seam.mock.MockFacesContext
但..
facesContext = new MockFacesContext(this.externalContext, this.application);
给出了编译错误
可能是我非常遗漏某些东西,在上面找到合适的在线示例。
以下是我的测试代码..
import org.jboss.seam.mock.*;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.runner.RunWith;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.log4testng.Logger;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
import javax.faces.el.ValueBinding;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
@RunWith(JMock.class)
public class WorkCaseConverterTest extends SeamTest {
@Test
public void testGetAsObject()
throws Exception {
new AbstractSeamTest.ComponentTest() {
public Mockery mockeryContext = new JUnit4Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
FacesContext mockfacesContext1 = this.mockeryContext.mock(FacesContext.class);
UIComponent mockUiComponent1 = this.mockeryContext.mock(UIComponent.class);
Application mockApplication1 = this.mockeryContext.mock(Application.class);
@Override
protected void testComponents() throws Exception {
ValueBinding vb = mockfacesContext1.getApplication().createValueBinding("WorkcaseType.class");
logger.debug("Getting bean....");
mockUiComponent1.setValueBinding("value",vb);
String value = null;
Object result = converter.getAsObject(mockfacesContext1, mockUiComponent1, value);
assertEquals(result, null);
}
}.run();
}
答案 0 :(得分:0)
你得到什么断言错误?
FacesContext mockfacesContext1 = this.mockeryContext.mock(FacesContext.class);
UIComponent mockUiComponent1 = this.mockeryContext.mock(UIComponent.class);
Application mockApplication1 = this.mockeryContext.mock(Application.class);
ValueBinding vb = mockfacesContext1.getApplication().createValueBinding("WorkcaseType.class");
如果您希望最后一行代码返回一个ValueBinding对象,那么这不会像写的那样工作 - 您需要在mockfacesContext1,mockUiComponent1&上设置期望值。 mockApplication1以返回ValueBinding对象:
context.checking(new Expectations() {{
oneOf(mockfacesContext1).getApplication();
will(returnValue(mockApplication1 ));
oneOf(mockApplication1).createValueBinding("WorkcaseType.class");
will(returnValue(vb));
}});
其中vb是具体实例或另一个模拟。但是,据我所知,问题是您尝试测试的方法甚至不执行.getApplication().createValueBinding("WorkcaseType.class");
您可以发布完整的测试代码吗?