Home > Blockchain >  How can i test the return value of a method, which builds an object?
How can i test the return value of a method, which builds an object?

Time:01-11

Hello im currently testing this method:

public Customer buildCustomer() {
    //Dinge für die Personengenerierung
    Customer customer = new Customer();
    return customer;
}

I build this test class:

class PersonenBuilderTest {

@Test
void buildCustomerReturnsCustomer() {
    var builderTest = new PersonenBuilder();
    builderTest.buildCustomer()

}}

What i want to test, is if the return value returns customer and not other things. How can i do this? And is it even necessary to test such a thing?

CodePudding user response:

When using Junit 5, you could do something like this :

@Test
void buildCustomerReturnsCustomer() {
    var builderTest = new PersonenBuilder();
    assertThat(builderTest.buildCustomer()).isInstanceOf(Customer.class)
}}

CodePudding user response:

Depending on what you are testing for, it is good practice to test methods. If you want to test if it returns an instance of Customer, you can use Junit as mention by @Kristof DB, and instead use the "asserEquals()" method.

class PersonenBuilderTest {
@Test
void buildCustomerReturnsCustomer() {
    var builderTest = new PersonenBuilder();
    var customer = builderTest.buildCustomer();
    assertEquals(customer.getClass(), Customer.class);
}

}

  • Related