lundi 30 novembre 2015

How to not Mock an object when using Mockito and CDI?

I have an object reads configuration properties like this:

@ApplicationScoped
    public class Configuration {

    @Inject
    @Config(value = "endpoint.base", defaultValue = "http://localhost:52885/consumers")
private String base;

    public String getBase() { return base; } 

}

this object is injected to a service object like this:

public class LoyaltyService {

    final Sender sender;

    final Configuration config;

    @Inject
    public LoyaltyService(Sender sender, Configuration config) {
        this.sender = sender;
        this.config = config;
    }
} 

I am now testing this service object with Mockito. I want to mock the Sender object, but I don't want to mock the configuration, or at least I just want to use the default value defined inside the object.

How can I do that in a Test object?

For example, I tried the following:

public class LoyaltyServiceTest {

    @Mock
    private Sender sender;

    @Inject
    private Configuration config;

    private LoyaltyService target;

    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
        when (sender.post(anyString(), anyString())).thenReturn("Post Success");
        target  =new LoyaltyService(sender, config);
    }
}

It doesn't seem CDI will register the Config object at all. How does this work? Thanks!

Aucun commentaire:

Enregistrer un commentaire