Seam 中的模拟 facescontext 和 uicomponent
我正在尝试在接缝中为以下方法编写单元测试。 为此......我需要模拟 facesContext 和 UIComponent 并将其传递给方法 getAsObject 。
我尝试使用 Jmock 和 seam 但遇到了问题。有什么建议吗?
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();
}
I am trying to write unit test for the following method in seam.
To do this…I need to mock both facesContext and UIComponent and pass it to method getAsObject .
I tried using Jmock and seam but running into issues. Any suggestions?
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());
}
}
}
}
Issues I ran into while using 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
I tried seam way by using.. org.jboss.seam.mock.MockFacesContext
but..facesContext = new MockFacesContext(this.externalContext, this.application);
gives compilation error
May be I am terribly missing something, dint find appropriate online examples on it.
Below is my test code..
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到什么断言错误?
如果您打算最后一行代码返回 ValueBinding 对象,那么这将不会像编写的那样工作 - 您需要对mockfacesContext1,mockUiComponent1和mockfacesContext1,mockUiComponent1和mockfacesContext1设置期望。 mockApplication1 以返回 ValueBinding 对象:
其中 vb 是具体实例或另一个模拟。但是,据我所知,问题是您尝试测试的方法甚至不执行
.getApplication().createValueBinding("WorkcaseType.class");
您可以发布您的完整的测试代码?
What assertion error are you getting?
If you intend the last line of code to return a ValueBinding object then this isn't going to work as written - you need to set expectations on mockfacesContext1, mockUiComponent1 & mockApplication1 in order to return a ValueBinding object:
where vb is either a concrete instance or another mock. But, as far as I can see, the problem is that the method you are trying to test doesn't even execute
.getApplication().createValueBinding("WorkcaseType.class");
Can you post your full test code?