Apr 12
2
Using GWT Autobeans in JUnit Tests
GWT AutoBeans
Where GWT AutoBeans Started
GWT has created an awesome framework called AutoBeans. It was originally created to be part of the backing to the GWT RequestFactory change management. The basics is that given an interface that extends ValueProxy which represents a bean (using the typical getters and setters), AutoBeans does the actual creation and managing of that bean.
The details aren’t really that important, just push the I believe button if you are using GWT’s Request Factory. AutoBeans is used to bridge the gap between the server side Entity beans and the GWT front end’s Data Transfer Objects. The problem that is introduced when using AutoBeans is using the interfaces in your JUnit Tests.
The Google Wizards actually made AutoBeans so that it can be used in GWT Applications as well as out of them, such as desktop and server apps. Due to this feature, you can also use them in your JUnit tests. Here is an example on how to do just that.
GWT AutoBeans JUnit Example
If you already are using RequestFactory, you probably already have your bean interfaces that are something like:
|
1 2 3 4 |
public interface MyBeanProxy extends ValueProxy {
String getName();
void setName(String name);
} |
In order to actually create your AutoBeans bean, you will need a factory interface:
|
1 2 3 |
package public interface BeanFactory extends AutoBeanFactory {
AutoBean<MyBeanProxy> createMyBean();
} |
Once the interface and the factory are in place, you can now use them in your tests:
|
1 2 3 4 5 6 7 8 9 |
@Test
public void shouldHaveABeanImplementation() {
BeanFactory factory = AutoBeanFactorySource.create(BeanFactory.class);
model = factory.createMyBean().as();
model.setName("GWTTutorial.com");
// put your assertions here
} |
There you have it, that is all you need to do to stub out your GWT AutoBeans interfaces in your JUnit Test Cases.
Happy Coding!
