@Test 注解

2022-11-01 11:16:56

@Test注解:

JUnit 4发布的新特性之一就是支持了@Test注解;
@Test的使用 是该方法可以不用在main方法中调用就可以测试出运行结果的一种测试工具。一般函数都需要有main方法调用才能执行,方便了开发人员在开发过程中测试代码功能。不过需要注意被测试的方法必须是public修饰的。

使用案例:

案例说明:自己的新建的类使用@Test注解时候,报错类似:Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project,这样的话就因为你当前类或者你当前的项目中有名为Test的类型,因为使用@Test注解的话,项目中不允许有与Test注解
重名的类!解决方法是将项目中原类名Test的类名修改为MyTest即可(修改的名称自己随意,符合类名规范就行);

以下正确案例(鼠标右击粘贴复制即可):

import org.junit.Test;

public class Hz20220318{
    @Test
    public void inputcheck(){
        System.out.println("ceshi@Test__hahha");
    }
    @Test
    public void outcheck(){
        System.out.println("this is me!!!");
    }
}

运行结果

this is me!!!
ceshi@Test__hahha

Process finished with exit code 0
  • 作者:无聊的HZ
  • 原文链接:https://blog.csdn.net/weixin_42188583/article/details/123574671
    更新时间:2022-11-01 11:16:56