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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With