繁体   English   中英

JUNIT 测试 - Eclipse IDE 抛出错误

[英]JUNIT test - Eclipse IDE is throwing an error

我有下面的类和方法,我试图让 JUNIT 测试工作,但它抛出了一个错误。 这是课堂

package christmas;

public class Worksheet1q1 {

    public static void main(String[] args) {
        int m = 3, n = 4;
        int result = power(m, n);
        System.out.printf( "power method  ");
        System.out.printf( "%d^%d = %d", m, n, result);


    } 

    //* code for  power

static int power(int m, int n) {



    if (n != 0)



        return (m * power(m, n - 1));
    else


 }}

这是我的 JUNIT 测试类和方法。

  package christmas;


  import static org.junit.Assert.assertThat;

  import org.junit.Test;



  public class Worksheet1q1Test {

    @Test


    public void twoAndThreeissix(int m, int n) throws Exception {




    int result = Worksheet1.power(2, 3);

    assertThat(result, is(8L));

    }}

我有2个问题。 首先,当我运行 Test 类时,我有一个错误,即方法“找不到圣诞节”,但圣诞节是我的包名,所以不确定为什么 Eclipse 认为它是一种方法

另外,我在 assertThat 上遇到错误 - 该错误表明找不到方法“is”。

这是我第一次尝试 JUNIT 测试,因此我们将不胜感激。

我运行了你的代码,并在他们玩游戏时修复了错误。 我在命令行上运行它。 我已将所有出错的内容放入评论中:

package christmas;

public class Worksheet1q1 {

    public static void main(String[] args) {
        int m = 3, n = 4;
        int result = power(m, n);
        System.out.printf( "power method  ");
        System.out.printf( "%d^%d = %d", m, n, result);
    } 

    //* code for  power
    static int power(int m, int n) {
      if (n != 0)
        return (m * power(m, n - 1));
      else return 1;    // added due to below error
    }                   // compile error, illegal start of statement.
}

对于测试,我添加了一个不会以int而不是long失败的测试:

package christmas;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.is; //  error: cannot find symbol

// compiler: warning: [deprecation] <T>assertThat(T,Matcher<? super T>) in Assert has been deprecated
// import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

public class Worksheet1q1Test {

    @Test
    // FAILURES!!!  Method twoAndThreeIsEightLong should have no parameters
//    public void twoAndThreeIsEightLong(int m, int n) throws Exception {
    public void twoAndThreeIsEightLong() throws Exception {
//      int result = Worksheet1.power(2, 3); // error: cannot find symbol
      int result = Worksheet1q1.power(2, 3);
      assertThat(result, is(8L));  // result is an int - will fail!
    }

    @Test
    public void twoAndThreeIsEightInt() throws Exception {
      int result = Worksheet1q1.power(2, 3);
      assertThat(result, is(8));
    }

    @Test
    public void twoAndThreeIsEight() throws Exception {
      int result = Worksheet1q1.power(2, 3);
      assertEquals(result, 8);
    }
}

编译:

javac -cp C:\..snip..\lib\junit-4.13.jar;C:\..snip..\lib\hamcrest-core-1.3.jar;target -d target src/christmas/*.java

跑步:

java -cp C:\..snip..\lib\junit-4.13.jar;C:\..snip..\lib\hamcrest-core-1.3.jar;target org.junit.runner.JUnitCore christmas.Worksheet1q1Test 

结果:

JUnit version 4.13
...E
Time: 0.042
There was 1 failure:
1) twoAndThreeIsEightLong(christmas.Worksheet1q1Test)
java.lang.AssertionError:
Expected: is <8L>
     but: was <8>
        at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
        at org.junit.Assert.assertThat(Assert.java:964)
        at org.junit.Assert.assertThat(Assert.java:930)
        at christmas.Worksheet1q1Test.twoAndThreeIsEightLong(Worksheet1q1Test.java:18)

FAILURES!!!
Tests run: 3,  Failures: 1

你的代码不好。

这在 IntelliJ 中工作正常:

package christmas;

public class Worksheet1q1 {

    public static final int DEFAULT_BASE = 3;
    public static final int DEFAULT_EXPONENT = 4;

    public static void main(String[] args) {
        int m = (args.length > 0 ? Integer.parseInt(args[0]) : DEFAULT_BASE);
        int n = (args.length > 1 ? Integer.parseInt(args[1]) : DEFAULT_EXPONENT);
        int result = power(m, n);
        System.out.printf("%d^%d = %d", m, n, result);
    }

    static int power(int m, int n) {
        if (n < 0) throw new IllegalArgumentException("negative powers are not allowed");
        if (n == 0) {
            return 1;
        } else {
            return (m * power(m, n - 1));
        }
    }
}

接下来我将编写单元测试:

package christmas;

import org.junit.Assert;
import org.junit.Test;

public class Worksheet1q1Test {

    @Test
    public void testPower_Success() {
        // setup
        int m = 2;
        int n = 3;
        int expected = 8;
        // exercise
        int actual = Worksheet1q1.power(m, n);
        // assert
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void testPower_ZeroPower() {
        // setup
        int m = 2;
        int n = 0;
        int expected = 1;
        // exercise
        int actual = Worksheet1q1.power(m, n);
        // assert
        Assert.assertEquals(expected, actual);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testPower_NegativePower() {
        // setup
        int m = 2;
        int n = -3;
        int expected = 1;
        // exercise
        Worksheet1q1.power(m, n);
    }
}

暂无
暂无

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

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