Auto-creation of mock variables when writing tests (or something similar)
I wonder what do people do in cases like:
I'm writing a test for a service class that depends on a lot of other classes, so I need to build it by passing various mocks to the constructor:
SomeClassUnderTest classUnderTest = new SomeClassUnderTest(dependency1, dependency2, dependency3, ...);
the question is, what is the best way to create everything quickly?
E.g.: I start by typing:
new SomeClassUnderTest(
at this point, I'd like to have a quick way to create mocks for all of the parameters, and use them, that is, for each param introduce a mock field in the test such as
@Mock DependencyType1 dependency1; // we use mockito for our mocks, but EasyMock is similar
and use that in the new clause:
new SomeClassUnderTest(dependency1,
then go on (or even do all of them in one go).
Do you guys have a quick solution for this workflow?
请先登录再写评论。
No, I'm afraid there is no built-in solution
Denis
Too bad, I guess a good IDEA-y way to do that would be:
1) when no valid variables are available for auto completion via ctrl-space and similar, just propose a name that matches the method param name,
2) make inspections such as "create field" executable for a number of symbols at once, not just one