2

I am new to GWT / Java and need some ideas on how to solve this (seems like it should be) simple problem.

I have a GWT object and I am trying to test a private method:

public class BarChart extends FlowPanel {

    public BarChart() {
        super();
        privateMethod();
    }

    privateMethod() { 
        //want to test this 
    }
}

I've tried JUnit but it needs to be a GWTTestCase since this object needs GWT.create(), and I've tried reflection but GWTTestCase doesn't support it.

2 Answers2

7

I know that it isn't going to be the answer that you are looking for but unit testing private methods is bad form.

You should likely refactor your code such that the effects of the private method can be tested/observed via the public API.

Since the code sample you provided is so short it is difficult to offer specific strategies, but I can make some general statements about the use-case.

The best way to test a private method is via another public method. If this cannot be done, then one of the following conditions is true:

  1. private method is unreachable (dead) code
  2. There is a design smell near the class that you are testing; the private method should still be having an observable effect on the class
  3. The method that you are trying to test should not be private; Perhaps changing the visiblity of the method is perfectly acceptable in this instance.

Additionally you can separate the UI code from the application logic using the GWT UIBinder. Once the UI has been separated out, you can use JUnit tests to test the remaining code. GWTTestcases are only for classes that require GWT.create(...) and they run very slowly (compared to junit), so it is best to structure the code such that the majority of the tests can be done using junit and then use gwttestcase to cover the remainder. If you can achieve this structure then it opens up additional avenues for testing using easymock/mockito/reflection etc.

pfranza
  • 3,292
  • 2
  • 21
  • 34
3

In GWT client (included GWTTestCase), you can call private or protected methods using JSNI, this is known as the violator pattern

Given a class with a private method

package com.example;
public class MyClass {
  private MyObjectReturn myMethod(MyObjectParam param) {
     return null
  }
}

Create a jsni method and access it

native MyObjectReturn myViolatorMethod(MyClass instance, MyObjectParam param) /*-{
  return instance.@com.example.MyClass::myMethod(*)(param);
}-*/;
Manolo Carrasco Moñino
  • 9,723
  • 1
  • 22
  • 27
  • 1
    I really do no think the user who posted the query got the joke on Violator Pattern!!!!! – appbootup Apr 12 '13 at 03:12
  • 2
    FYI, it's not a joke I din't invented anything, actually it's the name the GWT team gave to this, do a web search to know more about it: [1](http://penguinparens.blogspot.com.es/2008/01/design-patterns-ahoy-violator-pattern.html), [2](https://code.google.com/p/google-gin/issues/detail?id=162), [3](http://gwt-code-reviews.appspot.com/831801/) ... Or simply dive into the sources of the GWT core to see some references to it: [TreeMapViolator.java](https://code.google.com/p/google-web-toolkit/source/browse/trunk/user/test/com/google/gwt/emultest/java/util/TreeMapViolator.java) ... – Manolo Carrasco Moñino Apr 12 '13 at 05:32
  • I was curious and did read about it to get the inside joke. :) . I do not think the person posting the query did!!!! – appbootup Apr 12 '13 at 07:14