0

I have a Aspect that has an Autowired field config and in my unit test I would like to set a mock to config after defining mock behavior.

Aspect:

@Aspect
public class Aspect {

    @Autowired
    private Configuration config;

...

Unit Test:

@Autowired
private Configuration config;

@Before
public void setup() {
    when(config.getInt(TTL)).thenReturn(10000);
    // set confing to Aspect
}

I know spring has ReflectionTestUtils.setField for setting an autowired field, but I don't have a reference to the aspect object.

Nelson
  • 2,972
  • 3
  • 23
  • 34
  • How did you define your mocks? Also, it looks like you're using mockito and you may have a chance for an easy way with [Springokito](https://bitbucket.org/hadadil/springockito/wiki/springockito-annotations) but I have not had the chance of testing it with aspects so far... – Morfic Jul 05 '16 at 11:56
  • I am using Mockito – Nelson Jul 05 '16 at 13:31
  • I was also interested to know how you defined your mocks. Also, did you get the chance to look at Springokito? – Morfic Jul 06 '16 at 09:06
  • It is a bean in a configuration. I have not looked an Springokito yet. – Nelson Jul 06 '16 at 13:55
  • If it's a bean in a _test context_ then it should automatically pick it up. Alternatively with Springockito you should be able to replace an actual bean definition with an _inline-mock_ via the [@ReplaceWithMock](http://stackoverflow.com/questions/19354473/springockito-how-to) annotation if i remember correctly, without the need of additional contexts & all that stuff – Morfic Jul 06 '16 at 14:04

0 Answers0