idea intellij對Spring進行單元測試

1、加入Junit4及SpringJUnit4支持

<!-- junit -->
<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

<!-- spring-test -->
 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>

2、創建測試類

(wins:右鍵菜單------->轉到(G)------->測試(E) 可快捷在test包,相同目錄下建立相應的測試類)

(ios:IntelliJ IDEA提供了一個快捷操作Cmd + Shift + T作爲類和測試之間的導航。同時允許用戶在那裏創建一個測試類。)



3、生成的代碼如下:

package net.wll.web.service.impl;

import org.junit.Test;

import static org.junit.Assert.*;

public class DAreasServiceImplTest {

	@Test
	public void testSelectSubDistricts() throws Exception {

	}

	@Test
	public void testSelectSubDistricts0() throws Exception {

	}
}

4、增加Spring-test支持

package net.xuele.activity.service.impl;

import net.xuele.activity.domain.DAreas;
import net.xuele.activity.service.DAreasService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
/** 注入相關的配置文件:可以寫入多個配置文件 **/
@ContextConfiguration(locations={"classpath:META-INF/spring/application-services.xml",
		"classpath:META-INF/spring/applicationContext-persist.xml"
})
public class DAreasServiceImplTest {
	@Resource
	private DAreasService dAreasService;

	@Test
	public void testSelectSubDistricts0() throws Exception {
		List<DAreas> dAreases =  this.dAreasService.selectSubDistricts0();
		System.out.println(dAreases.size());
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章