0

I have build a kind of handler pattern which gives me specific handler types based on input parameter. The autowiring is at constructor level, which fills the container(HashMap) with respective Strategy beans. Here is the code. How can I assert this using Junit?

NOTE: Strategy is an interface and those are used by impl class.
@Component
public class StrategyHandler {

     private Map<String, Strategy> responses;
     
     @Autowired
     public StrategyHandler(Set<Strategy> responseSet) {
         responses = new HashMap<>();
         responseSet.forEach(strategy -> responses.put(strategy.getName(), strategy));
     }
     
     
     public Strategy getStrategy(String name) {
         return responses.get(name);
     }
}
mitali
  • 87
  • 8

1 Answers1

0

Fixed the UT. Here is the code:

RunWith(MockitoJUnitRunner.class)
public class StrategyHandlerTest {
    
    private Set<Strategy> responses = new HashSet<>();
    
    @InjectMocks
    @Spy
    private TestClass testThis;

    private StrategyHandler handler;
    
    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
    }
    
    @Test
    public void testGetStrategy() {
        responses.add(testThis);
        handler = new StrategyHandler(responses);
        Strategy strategy = handler.getStrategy("HELLO1");
        Assert.assertNotNull(strategy);
    }
}

class TestClassPlanningKnockout extends PlanningKnockout {}
mitali
  • 87
  • 8
  • We could have helped you and provided you with an answer earlier if you have had answered the question I added. – João Dias Nov 10 '21 at 18:00