Home > Mobile >  Comparing an array to a single string output using assertEquals in Java
Comparing an array to a single string output using assertEquals in Java

Time:10-11

So I would like to create a simple code that greets people according to the input. The difficulity I have that I have no idea how to compare a simple string with an array, using arrayEquals (or any equivalent). This is the way I have created the code - according to a previous project:

Test file:

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class test {
    @Test
    public void ShouldGreet() {
        assertEquals("Hello, my friend.", new GreetPeople().greeter(""));
        assertEquals("Hello, Bob.", new GreetPeople().greeter("Bob"));
    }
}

Actual code:

import java.util.Arrays;

public class GreetPeople {
    public String greeter(String[] names) {
        if (Arrays.stream(names).count() == 1) {
            return("Hello, "   names   ".");
        }
        return("Hello, my friend.");
    }

}

Any kind of help is well appreciated!

CodePudding user response:

The first patameter of greeter is an array:

    assertEquals("Hello, my friend.", new GreetPeople().greeter(new String[]{""}));
    assertEquals("Hello, Bob.", new GreetPeople().greeter(new String[]{"Bob"}));
  • Related