0

I have to mock jerseyclient which is being created in Constructor of subjected service. Subjected service is System under test injected via Spring's @Autowired.

In constructor of the service client=client.create() method is written. We can't change this code(Although this is a code smell). I want to mock the jersey client but it is in constructor of the service. I am not able to mock this

1 Answers1

-1

sooo... long story short.. admitting you use mockito, in your src for test you should have an applicationcontext for your test.. usually we define one programmatically so, something along those lines.. import the .xml file you use for test purpose (in my case i imported the one for the mailserver, for the connection and for the authentication) instead of the one i use for the "local" environmnet. After then define a method to setup each and every of your service. You might need to add a mock for your template resolver as well, but ultimately this all depends on your stack... So based on your approach the final thing might be a bit different, but ultimately you're gonna do something along the lines of what i outline below:

                                    @Configuration
                @ImportResource(value = { 
                        "classpath:applicationContext-jdbc-test.xml",
                        "classpath:applicationContext-ldap-test.xml",
                    "classpath:applicationContext-mail-test.xml"})
                public class ApplicationTestContext {

                    @Bean
                    public ObjectMapperWrapper objectMapperWrapper() {
                        return Mockito.mock(ObjectMapperWrapper.class);
                    }

                    @Bean
                    public YourService yourService() {
                        return new YourServiceImpl();
                    }
                    }
witchedwiz
  • 295
  • 2
  • 10