八、Maven下进行单元测试

八、单元测试

erp_dao工程添加junit

    <dependency>

        <groupId>junit</groupId>

        <artifactId>junit</artifactId>

        <version>4.9</version>

        <scope>test</scope>

    </dependency>

 

erp_dao工程src/test/java 下创建测试类

package erp_dao;

 

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

 

import cn.itcast.mavenweb.dao.DepDao;

 

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations={"classpath:applicationContext_dao.xml","classpath:applicationContext_datasource.xml"})

public class TestDepDao {

 

    @Autowired

    private DepDao depDao;

       

    @Test

    public void testList(){

        System.out.println("test!!");

        System.out.println(depDao.getList().size());

    }

   

}

 

右键点击你要测试的方法

选择右键菜单  Run as --> JUnit test

如果是绿色的显示表示通过测试

如果是红色的显示表示未通过测试

 

 

如果有测试类未通过,直接打包,会出现以下提示

表示该工程没有成功,因为测试未通过。也就是说打包操作是要求在所有的测试用例都通过的基础上才进行。

另外,通过我们观察,发现测试类和JUnit 并不会打包到war包中





posted @ 2018-04-08 13:50  zhaoxuan734  阅读(137)  评论(0编辑  收藏  举报