Home > OS >  junit5 test case for a method which returns nothing
junit5 test case for a method which returns nothing

Time:09-30

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 a null full name, and a Person with a known full name that's different from the PersonInfo first name. Assert that the Person full name equals the PersonInfo 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 the PersonInfo first name. Save the Personfull name in a variable. Assert that the Person full name is different from the PersonInfo 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. enter image description here

enter image description here

  • Related