【问题标题】:mock facescontext and uicomponent in seam在接缝中模拟 facescontext 和 uicomponent
【发布时间】:2011-11-11 15:58:30
【问题描述】:

我正在尝试在 seam 中为以下方法编写单元测试。 为此……我需要模拟 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();
    }

【问题讨论】:

  • 你遇到了什么问题?
  • 我已经编辑并写下了我遇到的问题。如果需要,我可以发布单元测试类。谢谢。
  • 如果您更喜欢 Mocks,可以使用 MyFaces Test。否则,您可以使用容器内测试框架。
  • Dar,我已经发布了我的完整测试课程。您能否详细说明或指导我参考解释容器内测试框架/ MyFaces Test 的资源,这将与 Seam 一起使用。

标签: java seam jmock uicomponents facescontext


【解决方案1】:

你得到了什么断言错误?

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");

你能发布你的完整测试代码吗?

【讨论】:

  • 是的..这是.getApplication().createValueBinding("WorkcaseType.class");的问题我已经添加了完整的测试类,我需要运行嵌入式jboss容器才能工作吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
  • 2012-01-24
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多