### Question:
DefaultFactoryMethodQuery implements FactoryMethodQuery { @Override public List<Method> getFactoryMethodsForType(SpecimenType type) { List<Method> factoryMethods = new ArrayList<Method>(); Method[] allMethods = type.getRawType().getMethods(); for (Method method : allMethods) { if (isStatic(method) && returnTypeIsAssignable(method, type) && !isCopyConstructorMethod(type, method)) factoryMethods.add(method); } if (this.comparator != null) { Collections.sort(factoryMethods, this.comparator); } return factoryMethods; } DefaultFactoryMethodQuery();  DefaultFactoryMethodQuery(Comparator<Method> comparator); @Override List<Method> getFactoryMethodsForType(SpecimenType type);  }### Answer:
@Test public void returns_all_factory_methods_for_a_given_class() { this.query = new DefaultFactoryMethodQuery(); List<Method> factoryMethods = this.query.getFactoryMethodsForType(SpecimenType.of(TypeWithFactoryMethod.class)); assertEquals(3, factoryMethods.size()); assertTrue(factoryMethods.contains(expectedMethodOneParameter)); assertTrue(factoryMethods.contains(expectedMethodTwoParameters)); assertTrue(factoryMethods.contains(expectedMethodTwoParametersReverse)); }
@Test public void sorts_methods_using_given_comparator_ascending_parameter_count() { this.query = new DefaultFactoryMethodQuery(new MethodParameterCountComparator()); List<Method> factoryMethods = this.query.getFactoryMethodsForType(SpecimenType.of(TypeWithFactoryMethod.class)); assertEquals(expectedMethodOneParameter, factoryMethods.get(0)); assertEquals(expectedMethodTwoParametersReverse, factoryMethods.get(1)); assertEquals(expectedMethodTwoParameters, factoryMethods.get(2)); }
@Test public void sorts_methods_using_given_comparator_descending_parameter_count() { this.query = new DefaultFactoryMethodQuery(new InverseComparator<Method>(new MethodParameterCountComparator())); List<Method> factoryMethods = this.query.getFactoryMethodsForType(SpecimenType.of(TypeWithFactoryMethod.class)); assertEquals(expectedMethodTwoParameters, factoryMethods.get(0)); assertEquals(expectedMethodTwoParametersReverse, factoryMethods.get(1)); assertEquals(expectedMethodOneParameter, factoryMethods.get(2)); }
@Test public void returns_factory_methods_where_return_type_is_assignable_to_declaring_type() { this.query = new DefaultFactoryMethodQuery(); List<Method> factoryMethods = this.query.getFactoryMethodsForType(SpecimenType.of(AbstractTypeWithFactory.class)); assertEquals(1, factoryMethods.size()); }
@Test public void ignores_factory_methods_where_a_singleton_parameter_is_assignable_from_return_type() { this.query = new DefaultFactoryMethodQuery(); List<Method> factoryMethods = this.query.getFactoryMethodsForType(SpecimenType.of(TypeWithCopyFactoryMethod.class)); assertEquals(1, factoryMethods.size()); }
@Test public void ignores_factory_methods_where_a_singleton_generic_parameter_is_assignable_from_return_type() { this.query = new DefaultFactoryMethodQuery(); List<Method> factoryMethods = this.query.getFactoryMethodsForType(new SpecimenType<GenericTypeWithCopyFactoryMethod<String>>() {}); assertEquals(1, factoryMethods.size()); }