Java JUnit 测试类 (netbeans) 上没有可运行的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20358526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:20:17  来源:igfitidea点击:

No runnable methods on JUnit test class (netbeans)

javaunit-testingnetbeans-7junit4runnable

提问by cherry

Help me please!

请帮帮我!

I have created an EmployeeTestclass to write to an Employeeclass but this error occurs before I can finish it. I had written the similar project before this project, it ran without errors. This is a very simple class as you can see below.

我创建了一个EmployeeTest类来写入一个Employee类,但是在我完成它之前发生了这个错误。我在这个项目之前写过类似的项目,它运行没有错误。这是一个非常简单的类,如下所示。

This is the error message:

这是错误消息:

initialization ERROR : No runnable methods
    -No runnable methods
    -java.lang.Exception
    -at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

This is EmployeeTestclass:

这是EmployeeTest类:

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

public class EmployeeTest {
Employee employee;
public EmployeeTest() {
}

@BeforeClass
public static void setUpClass() {
}

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {
    employee = new Employee("Austin", "Powers", 70000.00); 
}
public void testGetName(){
    String expected = "Austin Powers";
    String actual = employee.getName();
    assertEquals(expected, actual);


}
public void testGetSalary(){
    double expected = 70000.00;
    double actual = employee.getSalary();
    double marginOfError = 0.0001;
    assertEquals(expected, actual, marginOfError);
 }
public void testChangeSalary(){

    double percentIncrease = 5.00;
    employee.changeSalary(percentIncrease);
    double expected = 73500.00;
    double actual = employee.getSalary();
    double marginOfError = 0.0001;
    assertEquals(expected, actual, marginOfError);
  }
}

This is unfinished Employeeclass:

这是未完成的Employee课程:

class Employee {
private String firstName;
private String lastName;
private double salary;

public Employee(String austin, String powers, double d) {
    firstName = austin;
    lastName = powers;

}


String getName() {
    return firstName +" "+lastName;
      }

double getSalary() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

void changeSalary(double percentIncrease) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  }

}

采纳答案by EJK

You need to add annotations to your test methods as follows:

您需要向测试方法添加注释,如下所示:

@Test
public void testGetSalary(){

Unlike Junit 3, JUnit 4 relies on annotations, not method names to identify tests.

与 Junit 3 不同,JUnit 4 依赖于注释而不是方法名称来识别测试。