作者:NarratorWang | 来源:互联网 | 2023-01-20 15:29
我开始进行单元测试.我有一个方法,它给了我预期的结果,但是,JUnit说它正在生成空值.基本上,我的方法是生成偶数.我的代码如下:
我的课:
import java.util.ArrayList;
public class BasicCommandsReview {
private ArrayList listaDeNumeros = new ArrayList<>();
private ArrayList 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
我的考试班:
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
提前感谢所有可能的想法
1> Andy Turner..:
你的方法没有返回任何东西.他们不能:他们有void
返回类型:
void evenNumbersAsString(int num){
您甚至可以将方法作为方法参数调用的唯一原因是因为这是常规的.Java中的等价物:
assertEquals("Array par -OK", "2,4,6,8,10,12", commandReview.evenNumbersAsString(12));
将是编译时错误,因为第3个参数是类型void
.我不熟悉groovy,但看起来它正在处理一个void
方法,好像它返回Void
- 这是不可实例化的,所以总是如此null
.
你正在打印东西,而不是返回它.如果要检查结果assertEquals
,则必须使方法返回值,例如String
.