私はSpringの依存関係をSpring 3.1.1.RELEASEにアップグレードしましたが、私はspring-test-mvcを単体コントローラの単体テストに使用しようとしています。私はSpring REST Controller Test with spring-test-mvc frameworkで使用されているテクニックに従っています。なぜなら、その人のために働いたように思われるからですが、私はこれまでに失敗しています。私は、「私のテストコンテキストファイルに欠落しているmはいくつかの重要な設定があると思う。ユニットテストREST Controller with spring-test-mvc
私はエラーを取得していない。Hello World
が印刷されることは決してありませんので、私はそれが動作していないことを知っている理由は、(コントローラを参照してください)。私はここで何をしないのですか?
コントローラー:
@Controller
@RequestMapping("/debug")
public class DebugOutputController {
@RequestMapping(method = RequestMethod.POST)
public void saveDebugOutput(@RequestBody DebugOutput debugOutput, HttpServletResponse response) {
System.out.println("Hello World");
}
}
テストクラス:
@RunWith(SpringJUnit4ClassRunner.class) //this lets tests access Spring beans defined in the context config file
@ContextConfiguration(locations={"file:src/test/resources/itest/restAPITestContext.xml"}) //tells the test where to get configuration and beans to be used by the test.
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) //overrides the default stack of listeners
public class ITRestAPI{
@Autowired
private DebugOutputController debugOutputController;
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup(debugOutputController).build();
}
@After
public void tearDown() throws Exception {
}
@Test
public void shouldPerformPost() throws Exception {
this.mockMvc.perform(post("/debug"));
}
}
restAPITestContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<context:component-scan resource-pattern="*DebugOutputController*" base-package="com.company.project.servlet" />
</beans>
spring-test-mvcは本当に有望ですが、ドキュメントがありません。この時点ではREADME以外何も認識していますか? –
@MikePartridge私が発見したすべての情報はGithubサイトからのものです。 –