Is it possible to test a method that uses uiBinder XML elements with JUnit?

283 Views Asked by At

Here is my problem :

I have a method called desactiveNavLinks that desactive all widgets in my sideBarContainer (HTMLPanel), this method works perfecly. So here is the code of my uiBinder XML :

MainSideBarImpl.ui.xml :

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui' xmlns:b="urn:import:com.github.gwtbootstrap.client.ui">
<ui:style>
.centerDiv {
    text-align: center;
}

.logo {
    height: 15%;
    width: 15%;
}

.logoName {
    width: 65%;
}
</ui:style>
<g:HTMLPanel>
    <div class="sidebar-nav">

        <div class="{style.centerDiv}">
            <b:Image url="GA/img/img1.png" styleName="{style.logoName}"></b:Image>
        </div>
        <div class="sidebar-avatar big">
            <b:Image ui:field="userPic"></b:Image>
        </div>
        <g:HTMLPanel ui:field="sideBarContainer">

            <nav id="sidebar" class="sidebar nav-collapse collapse">
                <ul id="side-nav" class="side-nav">

                    <b:NavLink icon="HOME" ui:field="homeNavLink">Clients</b:NavLink>

                    <li class="panel">
                        <b:AccordionGroup heading="Client" ui:field="accordionGrp">
                        </b:AccordionGroup>
                    </li>
                    <li>
                        <b:base.IconAnchor icon="EDIT" ui:field="benchNavLink">Benchmark</b:base.IconAnchor>
                    </li>
                    <li>
                        <a class="nav-header can-active">
                            <i class="icon-star"></i>
                            Favoris
                        </a>
                    </li>
                </ul>
            </nav>
        </g:HTMLPanel>


    </div><!-- sidebar -->
</g:HTMLPanel>

The method that I want to test in MainSideBarImpl.java :

public void desactiveNavLinks() {
    //sideBarContainer is a HTMLPanel declared in the same Classe with @UiField annotation.
    for(int i = 0;i<sideBarContainer.getWidgetCount();i++){
        if(sideBarContainer.getWidget(i).getStyleName().contains("active")){
            sideBarContainer.getWidget(i).removeStyleName("active");
            sideBarContainer.getWidget(i).getParent().removeStyleName("active");
        }
    }
    for(SiteNavLinkEditor w : sitesEditor.getEditors()){
        w.setActive(false);
    }

}

Here is my UnitTest Method :

@RunWith(GwtMockitoTestRunner.class)
public class MainSideBarTest{
    @GwtMock
    private HTMLPanel sideBarContainer;
    private MainSideBarImpl widget;

    @Before
    public void setUp(){
        widget = new MainSideBarImpl();
    }

    @Test
    public void testDesactiveNavLinks(){
        System.out.println("Widgets count = "+sideBarContainer.getWidgetCount()); //That is printing 0 and should print all the widgets inside of the HTMLPanel
        widget.desactiveNavLinks();
    }
}

It's look like that the class is not instantiated with the UiBinder process, so my question is :

Is there a way to test a UiBinder widgets just with JUnit or the only way is to use GwtTestCase tests?

1

There are 1 best solutions below

3
On

Yes, you have to use GwtTestCase. This is because you want inspect the DOM that is being created by UiBinder. If you just wanted to test the behavior as defined in the Java class bound to the UiBinder template, then JUnit + gwtmockito would suffice.