我開始進行單元測試。我有一個方法,它給了我預期的結果,但是,JUnit說它正在生成空值。基本上,我的方法是生成偶數。我的代碼如下所示:一種方法在JUnit中生成預期結果,但它表示爲空
我的類:
import java.util.ArrayList;
public class BasicCommandsReview {
private ArrayList<Integer> listaDeNumeros = new ArrayList<>();
private ArrayList<Integer> evenNumbersAsString = new ArrayList<>();
void printEvenNumbers(int num) {
for (int i=1; i<=num; i++) {
if (i%2==0) {
listaDeNumeros.add(i);
}
}
int j=0;
while(j < evenNumbersAsString.size() - 1){
System.out.print(evenNumbersAsString.get(j) + ",");
j++;
}
System.out.print(evenNumbersAsString.get(j));
}
void evenNumbersAsString(int num){
int j=0;
for (int i=1; i<=num; i++) {
if (i%2==0) {
evenNumbersAsString.add(i);
}
}
while(j < evenNumbersAsString.size() - 1){
System.out.print(evenNumbersAsString.get(j) + ",");
j++;
}
System.out.print(evenNumbersAsString.get(j));
}
}
我的測試類:
import org.junit.Test
class BasicCommandsReviewTest extends GroovyTestCase {
BasicCommandsReview commandReview = new BasicCommandsReview()
@Test
void testEvenNumberAsString_Even() {
assertEquals("Array par -OK", "2,4,6,8,10,12", commandReview.evenNumbersAsString(12))
}
@Test
void testEvenNumberAsString_Odd() {
assertEquals("Array impar -OK", "2,4,6,8,10,12,14", commandReview.evenNumbersAsString(15))
}
}
日誌當我運行一個測試,我得到:
2,4,6,8,10,12
junit.framework.ComparisonFailure: Array par -OK
Expected :2,4,6,8,10,12
Actual :null
2,4,6,8,10,12,14
junit.framework.ComparisonFailure: Array impar -OK
Expected :2,4,6,8,10,12,14
Actual :null
at junit.framework.Assert.assertEquals(Assert.java:100)
at junit.framework.TestCase.assertEquals(TestCase.java:261)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1466)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.callStatic(StaticMetaClassSite.java:65)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:222)
at BasicCommandsReviewTest.testEvenNumberAsString_Odd(BasicCommandsReviewTest.groovy:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Process finished with exit code 255
在此先感謝對於所有可能來的想法
這是如何編譯的?您不能使用對'void'方法的調用作爲方法參數。 –
是的。這是Groovy –
與您的問題無關:刪除@Test註釋。您正在運行JUnit 3測試,因此您不應該爲您的方法添加@Test。它們是測試,因爲方法名稱以'test'開始。 –