Hot questions for Using Mockito in jsf

Question:

I have a simple method in my bean to handle cell edit event in primefaces datatable. Method looks like this:

public void onCellEdit(CellEditEvent event) {
    Object oldValue = event.getOldValue();
    Object newValue = event.getNewValue();

    User user = (User) ((DataTable) event.getComponent()).getRowData();
    if (!newValue.equals(oldValue))
    user.setUsername(oldValue.toString());
                     ....
}

How do I mock this: User user = (User) ((DataTable) event.getComponent()).getRowData();


Answer:

CellEditEvent mockEvent = Mockito.mock(CellEditEvent.class);
DataTable mockDataTable = Mockito.mock(DataTable.class);

Mockito.when(mockEvent.getComponent()).thenReturn(mockDataTable);
Mockito.when(mockDataTable.getRowData()).thenReturn(userObj);

Question:

I'm testing my JSF application (v2.2.14), but I've some difficulties because I'm not able to mock the UIViewRoot class. When I try to mock this class with Mockito I receive the error

java.lang.ExceptionInInitializerError 
...
...
caused by java.util.MissingResourceException: Can't find javax.faces.LogStrings bundle

I use the viewRoot in order to manage the java.util.Locale of the application and sometimes I need to read this information.

In which way can I mock this class in order to continue my test cases?

Thank you!


Answer:

At the moment I've wrapped all the code that depends from JSF in a class that now I can easily mock.

@Component
public class FacesComponent implements Serializable {

    public Locale getLocaleFromFrontend() {
        return FacesContext.getCurrentInstance().getViewRoot().getLocale();
    }
}

and then my test class is similar to this

    @Autowired
    @InjectMocks
    private LocalDateConverter localDateConverter;

    @Autowired
    @Spy
    private FacesComponent facesComponent;
...
...
MockitoAnnotations.initMocks(this);
Mockito.doReturn(null).when(facesComponent).getLocaleFromFrontend();