ssm框架junit簡單測試_我寫


第一步:導入相關jar包

主要是 junit包,和spring-test包

 1 <dependecy>
 2     <groupId>junit</groupId>
 3     <artifactId>junit</artifactId>
 4     <version>4.12</version>
 5 </dependecy>
 6 
 7 <dependecy>
 8     <groupId>org.springframework</groupId>
 9     <artifactId>spring-test</artifactId>
10     <version>${spring.version}</version>
11 </dependecy>

 

第二步:配置好各種配置文件

(這里測試用的和開發一樣的一套配置文件,且后面測試代碼中通過注解直接把配置文件的加載目錄指定到開發的resouces下,即第三步中的“src/main/resouces”)

 

第三步:編寫test父類

 1 @RunWith(SpringJUnit4ClassRunner.class)
 2 @WebAppConfiguration("src/main/resouces")
 3 @ContextConfiguration(locations={"classpath:Spring-config.xml","classpath:Spring-servlet.xml"})
 4 public class BaseTest{
 5     protected Logger log = LoggerFactory.getLogger(getClass());
 6     
 7     @Test
 8     public void test(){
 9         System.out.pringln(|"aaa");
10     }
11 }

 

其中,

"classpath:Spring-config.xml" 是后端 spring的配置文件,當然其中還可以引用包括各種其他配置文件,如dataSource.xml,mybatis.xml等
"classpath:Spring-servlet.xml" 是前端控制器的配置文件,主要是前台展示的各種資源向后台請求的配置,包括各種靜態資源的請求,攔截等配置

其他測試類,只要繼承BaseTest類,然后,在里面直接只用@Test注解寫測試方法即可,如

 1 public class UserServiceTest extends BaseTest{
 2     
 3     @Autowired 
 4     private IUserService userService;
 5     
 6     @Test
 7     public void test1(){
 8         User entity  = new User();
 9         List<User>  list = userService.selectList(entity);
10         log.dubug("selectList--------------------------【{}】",list.size());
11     }
12 }    

 


免責聲明!

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



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