title: Spring MVC testing conversion service when using ConverterFactory
body: <p>Initially I had a converter for almost every entity classes in my project. I decided to drop all those converters and created a ConverterFactory that will do all the conversion.</p>
  
  <pre><code>@Service("dataConverterFactory")
  public class DataConverterFactory implements ConverterFactory&lt;String, CommonEntity&gt; {
  
      private static final Logger logger = LoggerFactory.getLogger(DataConverterFactory.class);
  
      private final EntityManager em;
  
      @Autowired
      public DataConverterFactory(EntityManager em) {
          this.em = em;
      }
  
      @Override
      public &lt;T extends CommonEntity&gt; Converter&lt;String, T&gt; getConverter(Class&lt;T&gt; targetType) {
          return new GenericConverter(targetType);
      }
  
      private final class GenericConverter&lt;T extends CommonEntity&gt; implements Converter&lt;String, T&gt; {
  
          private final Class&lt;T&gt; targetType;
  
          public GenericConverter(Class&lt;T&gt; targetType) {
              this.targetType = targetType;
          }
  
          @Override
          public T convert(String text) {
              if (text != null &amp;&amp; !text.isEmpty() &amp;&amp; !text.equalsIgnoreCase("-1")) {//Ignore - to be determined later
                  try {
                      return (T) em.find(targetType, Integer.valueOf(text.trim()));
                  } catch (Exception e) {
                      logger.error(e.getMessage(), e);
                  }
              }
              return null;
          }
  
      }
  
  }
  </code></pre>
  
  <p>This works fine but my tests are failing when conversion is required. Here is a snippet from my test xml configuration file</p>
  
  <pre><code>&lt;bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"&gt;
          &lt;property name="converters"&gt;
              &lt;set&gt;
                  &lt;ref bean="dataConverterFactory"/&gt;
              &lt;/set&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="dataConverterFactory" class="com.xyz.converter.DataConverterFactory"/&gt;
  </code></pre>
  
  <p>Sample test</p>
  
  <pre><code>  mockMvc.perform(fileUpload("/complaint/new")
              .file(file)
              .param("customer", "1")
      )
  </code></pre>
  
  <p>In the above test,I am trying to submit a Complaint but it will fail because customer will be null.</p>
  
  <p>Parameter customer should be converted to class Customer which extends CommonEntity.</p>
  
  <pre><code>public class Complaint extends CommonEntity {
  
      @NotNull
      @ManyToOne
      @JoinColumn(name = "customer_id", updatable = false)
      private Customer customer;
  
  }
  </code></pre>
  
  <p>How do mock/set conversion service to work in my tests?</p>