【问题标题】:No runnable methods on JUnit test class (netbeans)JUnit 测试类(netbeans)上没有可运行的方法
【发布时间】:2013-12-03 18:10:39
【问题描述】:

请帮帮我!

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

这是错误信息:

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

这是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);
  }
}

这是未完成的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.
  }

}

【问题讨论】:

    标签: java unit-testing netbeans-7 junit4 runnable


    【解决方案1】:

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

    @Test
    public void testGetSalary(){
    

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

    【讨论】:

    • 真的很管用。非常感谢,我会记住的:)
    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多