MakeItEasy基础知识

http://code.google.com/p/make-it-easy/

A tiny framework that makes it easy to write Test Data Builders in Java

Person类为POJO类。


import static com.natpryce.makeiteasy.MakeItEasy.a;
import static com.natpryce.makeiteasy.MakeItEasy.make;
import static com.natpryce.makeiteasy.MakeItEasy.with;


import com.natpryce.makeiteasy.Instantiator;
import com.natpryce.makeiteasy.Property;
import com.natpryce.makeiteasy.PropertyLookup;

public class MakeiteasyTest {
    public static final Property<Person, String> name = Property.newProperty();
    public static final Property<Person, Integer> age = Property.newProperty();
    public static final Property<Person, Double> salary = Property
            .newProperty();

    public static final Instantiator<Person> Person = new Instantiator<Person>() {

        @Override
        public Person instantiate(PropertyLookup<Person> lookup) {
            Person person = new Person();
            person.setName(lookup.valueOf(name, "name"));
            person.setAge(lookup.valueOf(age, 1234));
            person.setSalary(lookup.valueOf(salary, 12.3));
            return person;
        }

    };

    public static void main(String[] args) {
        Person person = make(a(Person, with(name, "ni")));
        System.out.println(person.getAge());
        System.out.println(person.getSalary());
    }

}

 

以下为MakeItEasy部分代码

public class MakeItEasy {
    public static <T> Maker<T> a(Instantiator<T> instantiator, PropertyValue<? super T, ?> ... propertyProviders) {
        return new Maker<T>(instantiator, propertyProviders);

  public static <T> T make(Maker<T> maker) {
        return maker.value();
  public static <T,V,W extends V> PropertyValue<T,V> with(Property<T,V> property, W value) {
        return new PropertyValue<T, V>(property, new SameValueDonor<V>(value));
    }   

}

}

 

你可能感兴趣的:(Make)