ssm中進行junit測試
阿新 • • 發佈:2018-11-27
1.加入maven依賴
<!-- 單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.3.RELEASE</version> <scope>test</scope> </dependency>
2.建立基礎測試類,其餘的測試類都會繼承這個基礎測試類
package com.ishop.base; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * Created by GanBaby on 2018/10/26 * 配置spring和junit整合,junit啟動時載入springIOC容器 */ @RunWith(SpringJUnit4ClassRunner.class) //告訴junit spring配置檔案 @ContextConfiguration("classpath:spring/applicationContext-*.xml") public class BaseTest { }
這是我的配置檔案所在的目錄,因為有多個,所以用*全部掃描
3.建立測試類,繼承剛剛建立的BaseTest
package com.ishop.test; import com.ishop.base.BaseTest; import com.ishop.service.user.TcUserService; import org.springframework.beans.factory.annotation.Autowired; import java.util.List; import java.util.Map; /** * Created by GanBaby on 2018/10/26 */ public class Test extends BaseTest { @Autowired private TcUserService tcUserService; @Test public void test(){ List<Map<String, Object>> list = tcUserService.selectList(); System.out.printf("我執行了"+list.get(1).get("userSex")); } }
4.走一波
我用的是idea執行方式如下,直接點選類或者方法左邊的綠色箭頭都行,下面是圖解
看看結果
如果感覺這樣不好找結果,大家可以debug執行