I am using mean bean 'http://meanbean.sourceforge.net' to validate/test my beans. It works fine for most of the beans. But when a bean has arrays in it, it is failing with following error.
SEVERE: getFactory: Failed to find suitable Factory for property=[hostNames] of type=[class [I]. Please register a custom Factory. Throw NoSuchFactoryException.
org.meanbean.factories.ObjectCreationException: Failed to instantiate object of type [[I] due to NoSuchMethodException.
Following is my sample code.
public class Machine {
private String[] hostNames;
public String[] getHostNames() {
return hostNames;
}
public void setHostNames(String[] hostNames) {
this.hostNames = hostNames;
}
}
import org.junit.Test;
import org.meanbean.test.BeanTester;
public class TestBeanUtil {
@Test
public void test1(){
new BeanTester().testBean(Machine.class);
}
}
Any help on how to get rid of this error. I found one way by ignoring specific fields like below.
Configuration configuration = new ConfigurationBuilder().ignoreProperty("hostNames").build();
new BeanTester().testBean(Machine.class, configuration);
But My concern is is there any way that i can test without ignoring specific proper (or) ignore all the arrays in one shot ?
You can create a custom factory for your field:
class HostNamesFactory implements Factory<String[]> {
@Override
public String[] create() {
return new String[] {"host1", "host2", "host3"};
}
}
Then use this factory when you create your custom configuration and pass it to the bean tester:
Configuration configuration = new ConfigurationBuilder().overrideFactory("hostNames", new HostNamesFactory()).build();
new BeanTester().testBean(Machine.class, configuration);
I am agree this is not the perfect solution but at least the property getter and setter will be tested.
Maybe too late, but you can include in the AbstractJavaBeanTest the factories of each property that cannot be created.
Here is a sample when you need a factory for LocalDateTime and UUID
@Test
public void getterAndSetterCorrectness() throws Exception
{
final BeanTester beanTester = new BeanTester();
beanTester.getFactoryCollection().addFactory(LocalDateTime.class,
new LocalDateTimeFactory());
beanTester.getFactoryCollection().addFactory(UUID.class,
new ExecutionUUIDFactory());
beanTester.testBean(getBeanInstance().getClass());
}
Then you just define the custom factories that you want (in this example)
/**
* Concrete Factory that creates a LocalDateTime.
*/
class LocalDateTimeFactory implements Factory<LocalDateTime>
{
@Override
public LocalDateTime create()
{
return LocalDateTime.now();
}
}
/**
* Concrete Factory that creates a UUID.
*/
class ExecutionUUIDFactory implements Factory<UUID>
{
@Override
public UUID create()
{
return UUID.randomUUID();
}
}
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