1

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();
    }
Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94

1 Answers1

0

What assertion error are you getting?

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

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:

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

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?

blank
  • 17,852
  • 20
  • 105
  • 159
  • Yes..its the problem with `.getApplication().createValueBinding("WorkcaseType.class");` I have added the complete test class, do I need to run embedded jboss container for this to work? – Himalay Majumdar Sep 09 '11 at 19:54