gpt4 book ai didi

java - JUNIT 测试 - Eclipse IDE 抛出错误

转载 作者:行者123 更新时间:2023-12-01 18:40:43 27 4
gpt4 key购买 nike

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

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 类时,出现错误,方法“找不到 christmas”,但 christmas 是我的包名称,所以不确定为什么 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

关于java - JUNIT 测试 - Eclipse IDE 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59937213/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com