Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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"}));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading