简体   繁体   English

JUnit 测试复数。 错误我不明白

[英]JUnit testing complex numbers. Error I don't understand

I'm getting an error I don't quite understand.我收到一个我不太明白的错误。 My goal is to create a simple program that adds, subtracts, multiplies, and divides complex numbers.我的目标是创建一个简单的程序来加、减、乘、除复数。 My ComplexNumber class compiles correctly, but when I try to compile with the first test case of my Tester class it gives me this error:我的 ComplexNumber 类编译正确,但是当我尝试使用 Tester 类的第一个测试用例进行编译时,它给了我这个错误:

 ComplexNumberTest.java:37: error: method add in class ComplexNumber cannot be applied to given types; assertEquals(test1.add()==0); ^ required: ComplexNumber found: no argument reason: actual and formal arguments differ in length

This is my add method这是我的添加方法

import java.lang.ArithmeticException;

public class ComplexNumber{
private float a; //real
private float b; //imaginary

public ComplexNumber(float a,float b)
{
    this.a = a;
    this.b = b;
} //end constructor



public ComplexNumber add(ComplexNumber otherNumber){
    ComplexNumber newComplex;
    float newA = a + otherNumber.getA();
    float newB = b + otherNumber.getB();
    newComplex = new ComplexNumber(newA, newB);
    return newComplex;
}//end add

This is my Tester class这是我的测试员课程

import junit.framework.TestCase;

public class ComplexNumberTest extends TestCase{

private ComplexNumber test1;
private ComplexNumber test2;
private ComplexNumber test3;
private ComplexNumber test4;
private ComplexNumber test5;


public void setUp(){
    test1 = new ComplexNumber (1,-1);
    test2 = new ComplexNumber(2,2);
    test3 = new ComplexNumber(0,2);
    test4 = new ComplexNumber(3,1);
    test5 = new ComplexNumber(4,4);
}//end set up


/**
 * A method used to test the add method.
 * add two Complex numbers together
 * (ai+bi)=a+bi
 * 
 **/ 
public void testAdd()
{
    assertEquals(test1.add()==0);
    //assertTrue(test2.add()==4);
    //assertEquals(test3.add()==2);
    //assertEquals(test4.add()==3);
    //assertEquals(test5.add()==8);
}//end testAddition

I feel like the solution is fairly simple and I've just been staring at it for too long.我觉得解决方案相当简单,我只是盯着它太久了。 Thanks for any advice.感谢您的任何建议。

The error says it all:错误说明了一切:

ComplexNumberTest.java:37: error: method add in class ComplexNumber cannot be applied to given types;
    assertEquals(test1.add()==0);
                      ^
  required: ComplexNumber
  found: no argument
  reason: actual and formal arguments differ in length

It is saying that it found no argument when you tried to use add() .据说当您尝试使用add()时没有发现任何参数。 It also says that it requires a ComplexNumber in the argument.它还说它在参数中需要一个ComplexNumber

public ComplexNumber add(ComplexNumber otherNumber){
    ComplexNumber newComplex;
    float newA = a + otherNumber.getA();
    float newB = b + otherNumber.getB();
    newComplex = new ComplexNumber(newA, newB);
    return newComplex; }//end add

You defined add to require another ComplexNumber to be passed as an argument which you haven't done when you used it as test1.add() .您将add定义为需要将另一个ComplexNumber作为参数传递,而您在将其用作test1.add()没有这样做。

Additionally, based on the method signature of add , it will never return 0 as you have assumed in your assertion.此外,根据add的方法签名,它永远不会像您在断言中假设的那样返回 0。

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

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