導入相應的jar包
junit4 整個套裝,包括junit4.jar
和hamcrest-core.jar
struts2 需要導入的是 struts2-junit-plugin.jar
spring 需要導入的是 spring-test.jar
新建測試文件夾
在項目根目錄下新建test
文件夾,以后編寫的測試類應該和被測試類保持包名一致,并把一些spring的配置文件(例如applicationContext.xml
文件)放到test
目錄下
編寫測試類
在測試類的頭部寫下如下注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:applicationContext.xml")
并在測試類里添加待測試的對象
@Resource(name="defaultService")
private defaultService defaultService;
編寫測試方法
/**
* Method: updateArticleWithoutContent(String uuid, String title, String categories, String tags)
*/
@Test
public void testUpdateArticleWithoutContent() throws Exception {
String uuid = "";
String title = "";
String categories = "";
String tags = "";
String json = defaultService.updateArticleWithoutContent(uuid, title, categories, tags);
System.out.println(json);
}
測試結果如圖

測試成功