在JUnit测试中找不到符号 [英] Cannot find symbol in JUnit Test

查看:140
本文介绍了在JUnit测试中找不到符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用JUnit Test来检查我的类,但由于某种原因无法使它们正常工作.我正在尝试的非常简单的测试如下所示:

I have to use JUnit Test to check my classes and somehow can't get them to work. The very simple test I'm trying looks like this:

@Test
    public void points_shouldCreatInstance() {
        assertEquals(1.0f,2.0f, Point.Point(1.0f,2.0f));
    }

并且我正在尝试测试此类:

and I'm trying to test this class:

public class Point {

    float x;
    float y;

    public Point(float x, float y){
        this.x=x;
        this.y=y;
    }

    public float get_x(){
        return this.x;
    }

    public float get_y(){
        return this.y;
    }
...
}

但是Netbeans告诉我,它找不到Point.Point(1.0f,2.0f));中的符号(第二个) 我敢肯定这很明显,但是我找不到关于JUnit的任何书面文档.

But Netbeans tells me, that it can't find the symbol (second) Point in Point.Point(1.0f,2.0f)); I'm sure it's obvious, but I wasn't able to find any well written documentations about JUnit.

推荐答案

Point(1.0f,2.0f) 是构造函数调用,而不是static方法调用(您不应使用点运算符),因此可以不会像Point.Point(1.0f,2.0f))这样调用,这是不正确的.
在这里,为了测试Point类,您需要使用new运算符(如new Point(1.0f,2.0f))创建Point类对象.

Point(1.0f,2.0f) is a constructor call, not static method call (you should NOT use dot operator), so you can't call like Point.Point(1.0f,2.0f)), which is incorrect.
Here, in order to test the Point class, you need to create the Point class object using new operator like new Point(1.0f,2.0f).

下面的代码中用注释显示了测试Point类的正确方法:

The correct way to test the Point class is shown in the below code with comments:

     @Test
     public void points_shouldCreatInstance() {
       //Create Point object (calls Point class constructor)
       Point point = new Point(1.0f,2.0f);

        //Check x is set inside the created Point object
        assertEquals(1.0f, point.get_x());

        //Check y is set inside the created Point object
        assertEquals(2.0f, point.get_y());
    }

这篇关于在JUnit测试中找不到符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆