idea下maven項目下spring junit 測試用例


使用idea在編寫的類下右鍵Go->Test或者ctrl+shift+t,點擊create new test會在相應目錄下創建test類
別寫代碼如下
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/config/**/applicationContext*.xml" }, loader = GenericXmlContextLoader.class)
@Transactional
public class BaseImageTextInfoActionTest extends AbstractCommonAction {
    @Autowired
    private BaseImageTextInfoService baseImageTextInfoService;

    @Test
    public void queryById() throws Exception {
      //具體代碼
    }

}
@RunWith(value = SpringJUnit4ClassRunner.class)必須要寫,相當於提供了spring的環境
@ContextConfiguration(locations = { "classpath:/config/**/applicationContext*.xml" }, loader = GenericXmlContextLoader.class)
加載config下的所有目錄下的以applicationContext的xml文件,加載相應的配置文件
@Transactional  測試方法會進入事務管理

pom文件,引入junit的jar包,最好是4.10版本以上的
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
    <scope>test</scope>
</dependency>

 2017-08-03 20:19:26


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM