I have the below method, but I am not able to create a test case for this. Can you please help ?
I dont want to use Mocking.
class A {
public void setValue(PersonInfo a, Person b){
if (a.getFullname() == null) {
b.setFullName(a.getFirstName());
}
}
}
I want to see if I set a value for PersonInfo.firstname
then whether it's setting as the full name of the Person or not.
Tried the below approach but it's coming as null
class TestClasss{
@Autowired
A a;
public void TestValue(){
Person p = new Person();
PersonInfo pi= new PersonInfo();
pi.setFirstName("TEST");
a.setValue(pi,p);
assertEqual("TEST",p.getFullName());
}
CodePudding user response:
For this I would write a couple tests:
Test 1: Create a
PersonInfo
with anull
full name, and aPerson
with a known full name that's different from thePersonInfo
first name. Assert that thePerson
full name equals thePersonInfo
first name.Test 2: Create a
PersonInfo
with a non-null full name, and a Person with a known full name that's different from thePersonInfo
first name. Save thePerson
full name in a variable. Assert that thePerson
full name is different from thePersonInfo
first name and equal to the value of the variable in which you saved it.
CodePudding user response:
You can do something like below.
@Test
public void testA(){
A ao = new A();
PersonInfo a = new PersonInfo();
a.setFirstName("Name");
Person b = new Person();
//call you method here
ao.setValue(a, b);
// As values of b object changed in the method so it will be reflected after
// method call also
// simple assertion... based on your requirement update this assert statement
Assert.equals(a.getFirstName(), b.getFullName());
}
I also tried with same example .. I am getting values. I have attached one screenshot you can also verify.