Friday 15 April 2016

Mean Bean: Working with custom types

When mean bean finds any type, which is not recognizable, it creates a new instance of the type on the fly and set all the public properties. Only requirement is, this unrecognized type should have a default constructor.

Address.java
public class Address {
  private String city;
  private String country;
  private String pin;

  public String getCity() {
    return city;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public String getCountry() {
    return country;
  }

  public void setCountry(String country) {
    this.country = country;
  }

  public String getPin() {
    return pin;
  }

  public void setPin(String pin) {
    this.pin = pin;
  }

}


Employee.java
public class Employee {
  private String name;
  private int id;
  private Address address;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public Address getAddress() {
    return address;
  }

  public void setAddress(Address address) {
    this.address = address;
  }

}

import org.junit.Test;
import org.meanbean.test.BeanTester;

public class TestBean {

  @Test
  public void testEmployee() {
    BeanTester beanTester = new BeanTester();
    beanTester.setIterations(10);
    beanTester.testBean(Employee.class);
  }
}


Run TestBean.java, test will pass and gives you following log messages.
Apr 15, 2016 6:28:25 PM org.meanbean.factories.util.BasicFactoryLookupStrategy createTestedPopulatedBeanFactory
WARNING: Using dynamically created factory for [address] of type [mean_bean.mean_bean.Address]. Do you need to register a custom Factory?
Apr 15, 2016 6:28:25 PM org.meanbean.factories.util.BasicFactoryLookupStrategy createTestedPopulatedBeanFactory
WARNING: Using dynamically created factory for [address] of type [mean_bean.mean_bean.Address]. Do you need to register a custom Factory?
Apr 15, 2016 6:28:25 PM org.meanbean.factories.util.BasicFactoryLookupStrategy createTestedPopulatedBeanFactory
WARNING: Using dynamically created factory for [address] of type [mean_bean.mean_bean.Address]. Do you need to register a custom Factory?
Apr 15, 2016 6:28:25 PM org.meanbean.factories.util.BasicFactoryLookupStrategy createTestedPopulatedBeanFactory
WARNING: Using dynamically created factory for [address] of type [mean_bean.mean_bean.Address]. Do you need to register a custom Factory?
Apr 15, 2016 6:28:25 PM org.meanbean.factories.util.BasicFactoryLookupStrategy createTestedPopulatedBeanFactory
WARNING: Using dynamically created factory for [address] of type [mean_bean.mean_bean.Address]. Do you need to register a custom Factory?
Apr 15, 2016 6:28:25 PM org.meanbean.factories.util.BasicFactoryLookupStrategy createTestedPopulatedBeanFactory
WARNING: Using dynamically created factory for [address] of type [mean_bean.mean_bean.Address]. Do you need to register a custom Factory?
Apr 15, 2016 6:28:25 PM org.meanbean.factories.util.BasicFactoryLookupStrategy createTestedPopulatedBeanFactory
WARNING: Using dynamically created factory for [address] of type [mean_bean.mean_bean.Address]. Do you need to register a custom Factory?
Apr 15, 2016 6:28:25 PM org.meanbean.factories.util.BasicFactoryLookupStrategy createTestedPopulatedBeanFactory
WARNING: Using dynamically created factory for [address] of type [mean_bean.mean_bean.Address]. Do you need to register a custom Factory?
Apr 15, 2016 6:28:25 PM org.meanbean.factories.util.BasicFactoryLookupStrategy createTestedPopulatedBeanFactory
WARNING: Using dynamically created factory for [address] of type [mean_bean.mean_bean.Address]. Do you need to register a custom Factory?
Apr 15, 2016 6:28:25 PM org.meanbean.factories.util.BasicFactoryLookupStrategy createTestedPopulatedBeanFactory
WARNING: Using dynamically created factory for [address] of type [mean_bean.mean_bean.Address]. Do you need to register a custom Factory?


It generates 10 warning messages, it is because I set number of iterations to 10 (beanTester.setIterations(10);). Since Address class don’t provide any constructor, Java compiler provides you default constructor.

What if my Address class has no argument constructor and don’t provide default constructor?
Your test fails. Update Address.java like below.
public class Address {
  private String city;
  private String country;
  private String pin;

  public String getCity() {
    return city;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public String getCountry() {
    return country;
  }

  public void setCountry(String country) {
    this.country = country;
  }

  public String getPin() {
    return pin;
  }

  public void setPin(String pin) {
    this.pin = pin;
  }

  public Address(String city, String country) {
    this.city = city;
    this.country = country;
  }

}


Run TestBean.java, You will get following kind of error message.

Apr 15, 2016 6:31:48 PM org.meanbean.factories.util.BasicFactoryLookupStrategy createTestedPopulatedBeanFactory
SEVERE: getFactory: Failed to find suitable Factory for property=[address] of type=[class mean_bean.mean_bean.Address]. Please register a custom Factory.

Observe error message, test failed with message like “Please register a custom Factory”. I will explain about the creation and registration of custom factories later.




Previous                                                 Next                                                 Home

No comments:

Post a Comment