Robin
Robin

Reputation: 563

Unit test How to create a mock object for DynamicForm in play framework (java)

I have a controller which uses formfactory.form().bindFromRequest()

When I mock it, it throws a null pointer exception for

bindFromRequest()

ie.

when(formfactoryMock.form()).thenReturn(df.bind(params).bindFromRequest());

How to fix it?

Upvotes: 2

Views: 820

Answers (2)

capo11
capo11

Reputation: 854

Following the @g_piper's answer, in Play Framework 2.8 I solved the issue in this way:

Case 1

If you want get as separated fields and your code is something like:

DynamicForm form = formFactory.form().bindFromRequest(request);
String field = form.get("field_name");
@Mock
private FormFactory mockFormFactory;
@Mock
private DynamicForm mockForm;
@Mock
private DynamicForm mockDataForm;

@Before
public void setUp() {
    MockitoAnnotations.openMocks(this);
}

@Test
public void test() {
    when(mockFormFactory.form()).thenReturn(mockDataForm);
    when(mockDataForm.bindFromRequest(any())).thenReturn(mockForm);
    when(mockForm.hasErrors()).thenReturn(false); // I was calling hasErrors, You may not be...
    when(mockForm.get(anyString())).thenReturn("username"); // updatedRecord was the model that I needed to pull from the form.

    //continue with the test code
}

Case 2

If you want map the data as your POJO and your code is something like:

YourPojo coordinator = formFactory.form(YourPojo.class).bindFromRequest(request).get();
@Mock
private FormFactory mockFormFactory;
@Mock
private Form<Object> mockFormObject;
@Mock
private Form<Object> mockDataFormObject;

@Before
public void setUp() {
    MockitoAnnotations.openMocks(this);
}

@Test
public void test() {
    
   when(mockFormFactory.form(any(Class.class))).thenReturn(mockFormObject); 
when(mockFormObject.bindFromRequest(any())).thenReturn(mockDataFormObject);
    when(mockFormObject.hasErrors()).thenReturn(false);
    when(mockDataFormObject.get()).thenReturn(new YourPojo());

    //continue with the test code
}

Upvotes: 0

g_piper
g_piper

Reputation: 21

Here is how I was able to mock it out to get around a similar issue:

    import play.data.Form;
    import play.data.FormFactory;
    import com.fasterxml.jackson.databind.JsonNode;

    FormFactory mockFormFactory = mock(FormFactory.class);
    Form mockForm = mock(Form.class);
    Form mockDataForm = mock(Form.class);
    when(mockFormFactory.form(any(Class.class)))
            .thenReturn(mockDataForm);
    when(mockDataForm.bind(any(JsonNode.class)))
            .thenReturn(mockForm);
    when(mockForm.hasErrors())
            .thenReturn(false); // I was calling hasErrors, You may not be...
    when(mockForm.get())
            .thenReturn(updatedRecord); // updatedRecord was the model that I needed to pull from the form.

This is not mocking out the bindFromRequest() method as the OP asked about (it is mocking out the bind() method) but the concept should be the same, just replace when(mockDataForm.bind(any(JsonNode.class))) with something like when(mockDataForm.bindFromResult())

(or so I think).

Anyway, I was not able to find any examples of how to do this ANYWHERE... so hopefully this helps someone else.

Upvotes: 2

Related Questions