File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -301,3 +301,45 @@ class User {
301301}
302302```
303303
304+ #### Using ` @InTestsUseFactories `
305+
306+ The ` @InTestsUseFactories ` annotation allows the user to recommend specific factories to use in tests.
307+ This can be useful if Cover is not using the correct factory methods to construct objects.
308+
309+ Consider the following example. In the test sources, create a class ` Factory ` that is responsible for constructing
310+ ` Car ` objects from some external resource (such as a JSON file, or the like). If we annotate the ` CarPainter ` 's
311+ ` changeColor ` method with ` @InTestsUseFactories ` pointing to the ` Factory ` 's ` getFirstCar ` method, Cover will attempt
312+ to use that to create instances of ` Car ` objects for testing.
313+
314+ You are able to specify multiple method names in the annotation, as well as specifying it multiple times (you could
315+ specify a ` ColorFactory ` for instance).
316+
317+ ``` java
318+ public class CarFactory {
319+ private static final CarFactory INSTANCE = new CarFactory ();
320+ private final List<Car > cars;
321+
322+ private CarFactory () {
323+ // initialize the list of cars from some resource
324+ }
325+
326+ public static Car getFirstCar () {
327+ return INSTANCE . cars. get(0 );
328+ }
329+
330+ // and so on...
331+ }
332+ ```
333+
334+ ``` java
335+ import com.diffblue.cover.annotations.InTestsUseFactories ;
336+
337+ public class CarPainter {
338+ @InTestsUseFactories (className = " CarFactory" , methodNames = {" getFirstCar" })
339+ public static Car changeColor (Car car , Color color ) {
340+ car. setColor(color);
341+ return car;
342+ }
343+ }
344+ ```
345+
You can’t perform that action at this time.
0 commit comments