Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meanbean: failed to test bean with arrays

Tags:

java

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 ?

like image 404
Hari Krishna Avatar asked Apr 15 '16 07:04

Hari Krishna


2 Answers

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.

like image 128
perbellinio Avatar answered Oct 20 '22 05:10

perbellinio


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();
    }
}
like image 23
Rogelio Blanco Avatar answered Oct 20 '22 05:10

Rogelio Blanco