1
List<String> list = mock(List<String>.class);   

here is a invalid syntax,how to pass a List<String> type to mock()?

2
  • 1
    I thought all parametrized classes (like List<whatever>) only differ at compile time, and in run time it's just List, due to type erasure? So, what you need is syntactically List.class; no idea how do you pass the parametrization information to your mock. Commented May 5, 2012 at 5:25
  • You should state in the question that you are using mockito. Commented May 5, 2012 at 7:14

1 Answer 1

6

Short answer is you can't. At runtime it's just List.class due to type erasure. If your mocking framework needs to know the type parameter there must be a different way to pass it in.

Sign up to request clarification or add additional context in comments.

1 Comment

You don't. Right on the Mockito front page is an example of mocking LinkedList. It uses just LinkedList.class without any type parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.