ConversionService 在单元测试中不起作用(spring 3.2)

jpa*_*tti 4 java spring spring-mvc junit4 mockito

我有一个运行良好的 Web 应用程序。现在我正在尝试为其编写单元测试。我的网络应用程序有以下 conversionService

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="....Class1ToStringConverter"/>
                <bean class="....StringToClass1Converter"/>
            </list>
        </property>
</bean>
<mvc:annotation-driven  conversion-service="conversionService" />
Run Code Online (Sandbox Code Playgroud)

这很好用,当我提出请求时

/somepath/{class1-object-string-representation}/xx 
Run Code Online (Sandbox Code Playgroud)

一切都按预期工作(字符串被解释为 Class1 对象)。

我的问题是尝试向我的控制器编写单元测试。conversionService 只是没有使用,spring 只是告诉我

Cannot convert value of type [java.lang.String] to required type [Class1]: no matching editors or conversion strategy found
Run Code Online (Sandbox Code Playgroud)

到目前为止我的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/applicationContext.xml", "file:src/main/webapp/WEB-INF/jpm-servlet.xml"})
@WebAppConfiguration()
public class GeneralTest {

    @Autowired
    private WebApplicationContext ctx;
    private MockMvc mockMvc;
    private TestDAO testDAO = org.mockito.Mockito.mock(TestDAO.class);

    @Before
    public void setUp() throws Exception {
        Mockito.reset(testDAO);
        mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build();
    }

@Test
public void testList() throws Exception {
    final Test first = new Test(1L, "Hi", 10, new Date(), true);
    final Test second = new Test(2L, "Bye", 50, new Date(), false);
    first.setTest(second);

    when(testDAO.list()).thenReturn(Arrays.asList(first, second));

    mockMvc.perform(get("/jpm/class1-id1"))
            .andExpect(status().isOk())
            .andExpect(view().name("list"))
            .andExpect(forwardedUrl("/WEB-INF/jsp/list.jsp"));
}
Run Code Online (Sandbox Code Playgroud)

我缺少什么?谢谢

Vde*_*deX 6

模拟转换器像这样,

  GenericConversionService conversionService = new GenericConversionService();
  conversionService.addConverter(new StringToClass1Converter());



Deencapsulation.setField(FIXTURE, conversionService);
Run Code Online (Sandbox Code Playgroud)