0

Hi below is the legacy java code I have and I have to add new test cases to it.

@RunWith(JukitoRunner.class)
public class MyServiceTest {

    @Inject
    private MyService myService;


    public static class TestModule extends JukitoModule {

        @Override
        protected void configureTest() {
            install(new ArchaiusModule() {
                @Override
                protected void configureArchaius() {
                    MapConfig mapConfig = MapConfig.builder()
                                    .put("application.id", APP_ID).build();
                    bindApplicationConfigurationOverride().toInstance(mapConfig);
                }
            });
            bind(Config.class).to(TestConfig.class);
            install(new ABCModule());
        }

    }

MyService implementation MyServiceImpl has below constructor

@Inject
public MyServiceImpl(Config config) {
    this.isEnabled = config.isEnabled();
}

How do I mock Config. I am trying to test isEnabled should return false for one of test cases. New to mockito and guice . I tried @Mock but giving me null.

Sammy Pawar
  • 1,201
  • 3
  • 19
  • 38
  • Do you have try some ting like this: "Mockito.when(new MyService (any(Config .class).thenReturn()" ? – Paulo Filipe Dantas Feb 28 '19 at 15:53
  • @PauloFilipeDantas MyService is interface and MyServiceImpl is concrete class. I don't think we should be using MyServiceImpl in test. And MyServiceImpl has other params not shown above as constructor arguments. Is there a better way? I mentioned mockito but we using Jukito. – Sammy Pawar Feb 28 '19 at 16:24
  • SammyPawar try to look for something similar in your lib, something that intercepts the creation of a specific object and you can specify what the will return (eg. in Mokito you have the option to look for When New, and in powermock you have the option 'PowerMockito.whenNew(.class).withParameterTypes(.class).thenReturn();' ). If in this lib do not has anything similar, i suggest you to add mockito lib or create your Config class in a @before method and use it when you need in your tests. – Paulo Filipe Dantas Feb 28 '19 at 19:36

0 Answers0