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