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

最佳答案

您得到什么断言错误?

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对象,则此方法将无法按编写的方式工作-您需要对ockfacesContext1,mockUiComponent1和嘲笑Application1设置期望值才能返回ValueBinding对象:

context.checking(new Expectations() {{
    oneOf(mockfacesContext1).getApplication();
        will(returnValue(mockApplication1 ));
    oneOf(mockApplication1).createValueBinding("WorkcaseType.class");
       will(returnValue(vb));
}});


其中vb是具体实例或另一个模拟。但是,据我所知,问题在于您尝试测试的方法甚至没有执行.getApplication().createValueBinding("WorkcaseType.class");

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

10-07 20:36