Caio Vitor
Caio Vitor

Reputation: 3

A method generates the expected results in JUnit but it says it is null

I am starting in Unit test. I have a method and it is giving me the expected result, however, JUnit says it is generating null values. Basically, my method is generating even numbers. My code follows below:

My class:

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));
        }
}

My test class:

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))
    }
}

The log i get when i run a test:

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

Thanks in advance for all thoughts that may come

Upvotes: 0

Views: 1211

Answers (1)

Andy Turner
Andy Turner

Reputation: 140484

Your methods aren't returning anything. They can't be: they have void return type:

void evenNumbersAsString(int num){

The only reason you can even invoke the method as a method parameter is because this is groovy. The equivalent in Java:

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

would be a compile-time error, because the 3rd parameter is of type void. I'm not familiar with groovy, but it looks like it's treating a void method as if it returns Void - which is non-instantiable, and so always null.

You're printing stuff, not returning it. If you want to check the result with assertEquals, you have to make your methods return a value, e.g. String.

Upvotes: 6

Related Questions