Junit4使用示例

第一种,创建普通Java工程,手动创建一个起名lib的目录,复制Jar包到lib目录。然后在project Structure,加入jar包在工程

 

 

双击shift键,输入Test 

未成功添加

已经成功添加

 

 第二种,引入pom依赖,同时加入阿里云镜像仓库

 <repositories>
        <repository>
            <id>nexus-aliyun</id>
            <name>nexus-aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
 <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

 然后创建一个类,有各种方法,点击generate

 

package org.qiu;

/**
 * @author Aiver
 * @date 2022/11/27 11:18
 */
public class Calculate {
    public int add(int x,int y){
        return x+y;
    }
    public int mul(int x,int y){
        return x*y;
    }
    public float div(int x,int y){
        return x/y;
    }
    public int sub(int x,int y){
        return x-y;
    }
}

 

自动生成成功 ,debug运行

 

 

 其中关于除法,比如  1/2 

public float div(int x,int y){
        if (y ==0){
            throw new ArithmeticException("除数不能为0!!");
        }
        return x/(y*1.0f);
    }

上面这个结果就是  1/2 = 0.5

而下面就是 1/2 = 0

 public float div(int x,int y){
        if (y ==0){
            throw new ArithmeticException("除数不能为0!!");
        }
        return x/y*1.0f;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值