1

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

1 回答 1

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

您可以发布完整的测试代码吗?

于 2011-09-09T13:17:14.580 回答