jeudi 1 octobre 2015

Mocking @UiField when using GWTMock and Mockito

I am using GWT2.7 where I have a view that is something like this:

public class InContextSearchView
    extends ViewWithUiHandlers<InContextSearchUIHandlers>
    implements InContextSearchPresenter.MyView
{

    @UiField
    TextBox searchValueField;

    private final Widget m_widget;

    interface Binder
        extends UiBinder<Widget, InContextSearchView>
    {

    }

        @Inject
    public InContextSearchView( Binder binder )
    {
        m_widget = binder.createAndBindUi( this );
        searchValueField.addKeyPressHandler( new KeyPressHandler()
        {
            @Override
            public void onKeyPress( KeyPressEvent event_ )
            {
                if( KeyCodes.KEY_ENTER == event_.getNativeEvent().getKeyCode() )
                {
                    occSearchBtn.click();
                }
            }
        } );

    }

    public void something(){
        //some code
    }
}

For this I have written a Mockito test case as given below:

@RunWith( GwtMockitoTestRunner.class )
public class TestInContextSearchView
{

    /**
    @GwtMock
    InContextSearchView.Binder m_binder;

    @InjectMocks
    @Spy
    InContextSearchView m_inContextSearchView;

    @Test
    public void testSomething()
    {
        m_inContextSearchView.something();
    }
}

The problem is "searchValueField" is not getting mocked and is null. So when mockito is trying to use constructor to create object for InContextSearchView it is throwing nullpointer exception. I am not supposed to touch original code. Also, PowerMock cannot be used if that can provide a solution.

What am I doing wrong? How can I initialize @UiField so nullpointer exception is avoided?

Aucun commentaire:

Enregistrer un commentaire