我有一个运行良好的 Web 应用程序。现在我正在尝试为它编写单元测试。我的 webapp 有以下转换服务
<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" />
哪个很好用,当我提出请求时
/somepath/{class1-object-string-representation}/xx
一切都按预期工作(字符串被解释为 Class1 对象)。
我的问题是尝试向我的控制器编写单元测试。只是没有使用转换服务,春天只是告诉我
Cannot convert value of type [java.lang.String] to required type [Class1]: no matching editors or conversion strategy found
到目前为止我的测试:
@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"));
}
我缺少什么?谢谢