Surya Pratap
Surya Pratap

Reputation: 11

How to inject a List or any non-mockable object into @Tested Spring beans in a JMockit testcase

I am trying to use JMockit- 1.45 to test spring based services. Strangely, I am unable to mock List objects. I have also noticed similar behavior for other non-mockable classes mostly from java.util.*. How can I achieve the below mentioned in Jmockit?

class ServiceA{
 @Autowired
 private List<SomeObj> list;
 .......
}

Class ServiceATest{
  @Injectable
  private List<SomeObj> list;
  @Tested
  private ServiceA serviceA;
  .............
}

I am getting below error

Caused by: java.lang.IllegalArgumentException: java.util.List is not mockable

Upvotes: 1

Views: 938

Answers (1)

Rog&#233;rio
Rog&#233;rio

Reputation: 16390

Use @Tested on the List field, initializing it with the actual list to be injected.

Upvotes: 0

Related Questions