java - mocking an autowired object in a test -


i have class has field gets set through spring bean , annotated @autowired so:

public class foo {     ....     @autowired     private bar bar;     ....  } 

i want setup junit test tests functionality @ point create foo object. not in control of when foo object generated. easiest way mock object anytime foo gets instantiated, object populated mocked item? using 3rd party library mock bar object nicely, prefer of in java test class without using xml files.

everything have found online seems either involve creating xml file or mocking specific instance of class. thank help!

i following:

public class test {     private bar mockedbar = createbar();     // somehow set 'mockedbar' foo object     // created contains mockedbar  } 

i have written uch test of jmockit,

please read might you.

you need create object overriding required methods.

then use

  deencapsulation.setfield(fixture,new bar(){      //@override methods  }); 

fixture object in want bar instance available mocked methods.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -