方法在JUnit中生成预期结果,但它表示它为null

时间:2017-08-31 15:28:11

标签: java unit-testing intellij-idea groovy 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

提前感谢所有可能出现的想法

1 个答案:

答案 0 :(得分:6)

你的方法没有返回任何东西。他们不能:他们有void返回类型:

void evenNumbersAsString(int num){

你甚至可以将方法作为方法参数调用的唯一原因是因为这是常规的。 Java中的等价物:

assertEquals("Array par -OK", "2,4,6,8,10,12", commandReview.evenNumbersAsString(12));

将是编译时错误,因为第3个参数的类型为void。我不熟悉groovy,但看起来它正在处理void方法,好像它返回Void - 这是不可实例化的,所以总是{{1} }。

您正在打印内容,而不是将其返回。如果您想使用null检查结果,则必须让您的方法返回一个值,例如assertEquals