gpt4 book ai didi

java - Seam 中的模拟 facescontext 和 uicomponent

转载 作者:行者123 更新时间:2023-12-01 05:40:25 26 4
gpt4 key购买 nike

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

我尝试使用seam方式.. 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 对象,那么这将不会按照编写的方式工作 - 您需要对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");

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

关于java - Seam 中的模拟 facescontext 和 uicomponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7326020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com