jeudi 27 octobre 2016

How to set in JMockit mocked properties to tested object on @Before method?

In a JMockit test, I have the following code:

@Tested
private PromotionsAddOrUpdateEntryStrategy strategy;

@Mocked
private BuyXGetYPromoPreAddOrUpdateEntryCommand precommand;

@Before
public void setUp()
{
    initializeCommands(precommand);
}

protected void initializeCommands(final BuyXGetYPromoPreAddOrUpdateEntryCommand command)
{
    final List<AddOrUpdateEntryCommand> commands = new ArrayList<>();
    commands.add(command);
    strategy.setPrecommands(commands);
}

When the test is executed, then I get a NullPointerException in strategy object. Why does it happen? And what is the correct way to do this? The idea is to avoid the repetition of the initializeCommands method in all tests.

Aucun commentaire:

Enregistrer un commentaire