简体   繁体   English

Netbeans上的Junit不起作用

[英]Junit on Netbeans doesn't work

I know that netbeans has an existing junit plugin but for some reason it doesn't work properly with my code: 我知道netbeans有一个现有的junit插件,但是由于某种原因,它不能与我的代码一起正常工作:

public int Add(String a, String b){
  int x = Integer.parseInt(a);
  int y = Integer.parseInt(b);
  return x + y;
}

Junit test class was generated that contains the following test case: 生成的Junit测试类包含以下测试用例:

public void testAdd() {
  System.out.println("Add");
  String a = "2";
  String b = "3";
  TestingProject2016 instance = new TestingProject2016();
  int expResult = 5;
  int result = instance.Add(a, b);
  assertEquals(expResult, result);
}

The junit shows that this test fails saying that there is initialization error, although it should be a success since it returns a correct value based on the code above. junit显示此测试失败,表明存在初始化错误,尽管应该成功,因为它会根据上面的代码返回正确的值。

在此处输入图片说明

Any idea what went wrong? 知道出了什么问题吗?

Note: that I am using Junit 4. Thanks :) 注意:我正在使用Junit4。谢谢:)

From the comments, the fix was to add the @Test annotation to the test method. 根据注释,解决方法是将@Test批注添加到测试方法。

The @Test annotation on a method is used to tell JUnit 4 that that method is a test. 方法上的@Test注释用于告诉JUnit 4该方法是一个测试。 You had no such annotations, so JUnit found no tests to run, and hence reported an error message with the text 'no runnable methods'. 您没有这样的注释,因此JUnit找不到要运行的测试,因此报告了一条错误消息,内容为“无可运行方法”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM