Reputation: 73
I have written a code for Person
class and in that class create a constructor Person
and argument name
. Now I have to create an instance of this class will offer a getter for the person's name and also create an instance of this class will respond to a greet
method that accepts one argument: message.
When the message is "Hello", greet must return:
Hi, I'm {{name}}
When the message is "Goodbye", greet must return:
Bye
When the message is anything else, greet
will return the message that it was provided. I have a tested case code but I am stuck with assertEquals()
function and getter function. Now I am facing error with assertfunction
. Can anybody please tell me how does assertfucntion
and getter works? I have implemented getter in my code, I'm but not sure whether I did it right.
Here's my code:
class Person
{
private String name;
Person(String n)
{
n = name;
}
String GetName()
{
return this.name;
}
public void greet(String t)
{
if (t == "Hello")
{
System.out.println("Hi my name is "+name);
}
else if (t == "Goodbye")
{
System.out.println("bye");
}
else
System.out.println("Hi, my name is"+name);
}
}
Test code:
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.Before;
public class TestPerson {
private Person jeff;
private Person susy;
@Before
public void setup() {
jeff = new Person("Jeff");
susy = new Person("Susy");
}
@Test
public void testGetName() {
assertEquals("Jeff", jeff.GetName());
}
@Test
public void testGreetHelloJeff() {
assertEquals("Hi, I'm Jeff", jeff.greet("Hello"));
}
@Test
public void testGreetHelloSusy() {
assertEquals("Hi, I'm Susy", susy.greet("Hello"));
}
@Test
public void testGreetGoodbye() {
assertEquals("Bye", jeff.greet("Goodbye"));
}
@Test
public void testGreetOther() {
assertEquals("Yo", jeff.greet("Yo"));
}
}
Upvotes: 1
Views: 323
Reputation: 5151
It doesn't make sense to test the greet(String)
method using assertEquals
, since the method doesn't return anything. The assertEquals
is used to assert whether the actual result returned from a tested method is equal to an expected value that you provide, just like the testGetName
above.
It seems more appropriate to change void greet(String)
to String greet(String)
which returns a greeting message according to the t
argument to make your code more testable.
Moreover, you should use aStr.equals(bStr)
instead of aStr == bStr
to compare two String. ==
just compare the reference of the two String object rather than their values.
Upvotes: 0
Reputation: 607
See your method returns void:
public void greet(String t)
How do you expect to get and assert a values of void?
Change void to String and do return a message string. Also do not use == rather .equals(..)
"Hello" case seems does the same as default. Better do:
public class Person {
private String name;
Person(String name) {
this.name = name;
}
String getName() {
return this.name;
}
public String greet(String m) {
if ("Goodbye".equals(m)) {
return "bye";
} else {
return String.format("Hi, my name is %s", name);
}
}
}
Upvotes: 1
Reputation: 2562
By calling assertEquals("Bye", jeff.greet("Goodbye"));
you're comparing the String "Bye" to void
since .greet
returns void
.
Change greet to this:
public String greet(String t){
if(t.equals("Hello"))
return "Hi my name is " + name;
else if(t.equals("Goodbye"))
return "bye";
else
return "Hi, my name is" + name;
}
And then you can use the assertEquals(String, String)
like:
assertEquals("bye", jeff.greet("Goodbye"));
In Java when comparing Strings use .equals() instead of ==. So 'if(t=="Hello")"' would be 'if(t.equals("Hello"))'
Method names should start with a lower case letter.
Also String comparison is case sensitive so make sure you are using the correct case when comparing your Strings.
Upvotes: 0
Reputation: 308763
You cannot compare a String
and the return value of a method that returns void
.
Your Person class is odd. You have it too closely tied to System.out, which is not useful.
Your code has a lot of problems for such a small sample size. Here's what I might suggest:
public class Person {
private String name;
Person(String n) {
this.name = n;
}
String getName() {
return this.name;
}
public String greet(String t) {
if ("Hello".equals(t)) {
return String.format("Hi my name is %s", name);
} else if ("Goodbye".equals(t)) {
return "bye";
} else {
return String.format("Hi, my name is %s", name);
}
}
}
Upvotes: 3