Spring-单元测试 demo

摘要:
记录一下自己用过的单元测试方法

-1 注意

  1. 加了@test的类 不能有返回值和参数

0 中间遇到的bug

中间遇到一个maven插件的问题
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-cli) on project spring_junit_module: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
可能的解决办法 用法二
https://blog.csdn.net/u011781521/article/details/74868680
其实后来发现,ide下面的event log里面也有报错,重启了一次ide

1.法一 用IntelliJ

优点:简单
缺点:不能批量处理吧

可参考:
https://blog.csdn.net/yangshijin1988/article/details/63262400
在这里插入图片描述

在这里插入图片描述

2 法二 使用spring junit

优点:直接加注释就行了
缺点:配置有点麻烦

package test;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@ContextConfiguration(locations={"classpath:applicationContext.xml","classpath:dispatcher-servlet.xml"}) //加载配置文件
@RunWith(SpringJUnit4ClassRunner.class)
public class BaseJunit4Test  {
}

package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@ContextConfiguration(locations={"classpath:applicationContext.xml","classpath:dispatcher-servlet.xml"}) //加载配置文件
@RunWith(SpringJUnit4ClassRunner.class)
public class test extends BaseJunit4Test{

    @Test
    public void test (){
       System.out.print("ok");
    }

}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--其实要不要都无所谓-->
    <context:annotation-config/>
    <context:component-scan base-package="test" />
</beans>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>spring_junit_module</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version> 3.2.4.RELEASE  </version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>


    </dependencies>
    
    <!--这是一个插件 不加可能会报错-->
      <!--其实要不要都无所谓-->
    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>



    </build>






</project>


我的具体操作:

1.新建一个springmvc项目
2.添加maven支持
3.修改application,xml 并把这两个文件放到resource目录下
4.增加和修改上面的文件
5.运行 :直接点击方法左边的运行按钮就行 无需启动整个项目在这里插入图片描述

项目源码:

项目1:
https://github.com/sunny73/WEB_Projects/commit/11438da6256fce670816fe33c82c314e0b69dffc

项目2:
https://github.com/sunny73/WEB_Projects/commit/7ab17ff76c072bf4a37a2bd74857864e7b6ee1d0

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值