#Testing strategy pattern Spring Boot

17 messages · Page 1 of 1 (latest)

uneven dragon
#

I have a problem, because I have no idea how to properly test map of @Component.

I mean i have a strategy pattern (implements of interface, which are components) . In Service i have a map of these.

Im using mockito, and i dont know, should i Mock this map or add elements to this in @BeforeEach.

For example:

@Component
@RequiredArgsConstructor
public class Service {

    private final Map<String, Strategy> strategies;

Test:

@ExtendWith(MockitoExtension.class)
public class ServiceTest {

    @Mock
    private Map<String, Strategy> strategies;

    private Service service;

    @BeforeEach
    public void init() {
        service = new Service(strategies);
    }
oak cradleBOT
#

This post has been reserved for your question.

Hey @uneven dragon! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

uneven dragon
#

I just need to know how to test strategy pattern in Spring Boot

fierce plover
#

test each strategy individually, and see if the correct strategy is resolved in your servicetest

uneven dragon
#

Im doing this too

uneven dragon
#

Just have no idea how to test Service with map of components

#

I have tests in Service now, they are working. But i have no idea the idea of mocking map is good

#

BTW if i use Injectmocks on Service field, tests wont be passing

fierce plover
#

don't mock the map, initialize the map in the constructor, because this should be filled with spring's dependency injection

#

So just add, dunno, one or two strategies, not even real implementations, the strategies can be mocked

#

And then test if the resolving of those strategies work

#

I have no idea what the rest of your code in that service does, so I'm just going off with what you're giving me and how I'd test that

uneven dragon
#

In Service im just creating objects, getting the type of strategy and create object with using strategy

#

But thanks, that what i needed

oak cradleBOT
# uneven dragon But thanks, that what i needed

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.