Testing the Builder Template

I am trying to create a unit test builder style template that looks like this (mockedObject is injected into a function, so I can mock it):

Thing thing = mockedObject.createThing() .addParam1() .addParam2(mockedObject.createParam2() .someData() .build()) .build(); mockedObject.use(thing); 

I am trying to figure out how to test this (using JUnit and Easymock) without having to mock the entire builder. All I need to check on the resulting object is that param1 and param2 were set with certain values.

Erupting the entire builder pattern would make the test extremely fragile, and its not something I should test for a start.

Is there a better way to test this?

+4
source share
1 answer

I am trying to figure out how to test this (using JUnit and Easymock) without having to mock the entire builder. All I need to check on the resulting object is that param1 and param2 were set with certain values.

Erupting the entire builder pattern will make the test extremely fragile and not something I should test for a start.

What behavior are you trying to test here? If you test that the builder is building the object correctly, the builder is not something you should taunt.

Perhaps the problems will be clearer if you publish the method you are testing and a more complete bit of test code, but right now this is not at all like a test.

0
source

Source: https://habr.com/ru/post/1337888/


All Articles