Skip to content

Commit 2038be8

Browse files
authored
Improve Cartesian test documentation (#665 / #666)
`@CartesianTest.MethodFactory`-annotated methods can be non-static under certain circumstances, but the demo snippets showed non-static factory methods that didn't match these and were thus wrong. This change fixes that, which required moving the factory methods from non-static inner classes into the outer class. For conistency and to make things easier in the future, the same was done for all other snippets as well. The snippets were prepended with imports, which was incoherent when the snippets turned from classes to just methods. Hence the imports were removed as well, which has the eadded benefit of not having to maintain the imports and making the snippets shorter. Closes: #665 PR: #666
1 parent 01713d7 commit 2038be8

File tree

4 files changed

+167
-269
lines changed

4 files changed

+167
-269
lines changed

docs/cartesian-product.adoc

Lines changed: 12 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,21 @@ NOTE: The CartesianTest extension has undergone significant changes in 1.6.0.
2727
This included moving it into a new package.
2828
link:/docs/cartesian-product-v1[The old variant] is deprecated and will be removed in the 2.0 release (tentatively scheduled for 2022).
2929

30-
== Basic Use
30+
== Basic use
3131

3232
`@CartesianTest` is used _instead_ of `@Test` or other such annotations (e.g. `@RepeatedTest`).
3333

3434
You can supply test parameters to `@CartesianTest` in two ways:
3535

36-
- You can annotate your test method, providing all parameter values in a single annotation.
37-
- The test parameters can be annotated with `@CartesianTest.Values`, `@CartesianTest.Enum`, or range source annotations (see <<Annotating your test parameters>>)
36+
- You can annotate the test parameters directly with `@CartesianTest.Values`, `@CartesianTest.Enum`, or range source annotations (see <<Defining arguments on parameters>>)
37+
- You can annotate test method itself with `@CartesianTest.MethodFactory` to point at a factory method that creates sets of arguments (see <<Defining arguments with factories>>)
3838

3939
Specifying more than one kind of parameter source (i.e.: annotating your test parameters and the test method itself) does not work and will throw an `ExtensionConfigurationException`.
4040

4141
Our earlier example with `{ 1, 2 }` and `{ 3, 4 }`, would look like this:
4242

4343
[source,java,indent=0]
4444
----
45-
import org.junitpioneer.jupiter.cartesian.CartesianTest.Values;
46-
4745
include::{demo}[tag=cartesian_simple_demo]
4846
----
4947

@@ -56,21 +54,14 @@ If your input is `{ 1, 1, 3 }` and `{ 2, 2 }` the extension will consider their
5654
Otherwise, the test would run with the same parameters multiple times.
5755
If you need to pass the same parameters multiple times, you might want to look into https://junit.org/junit5/docs/current/user-guide/#writing-tests-repeated-tests[repeated tests].
5856

59-
== Annotating your test parameters
57+
== Defining arguments on parameters
6058

6159
You can annotate the parameters of your `@CartesianTest`, to provide values to that specific parameter.
6260
Parameter annotations are "self-contained", they only provide values to the parameter they are on and do not interfere with each other.
6361
You can mix and match parameter annotations as you need.
6462

6563
[source,java,indent=0]
6664
----
67-
import java.time.temporal.ChronoUnit;
68-
69-
import org.junitpioneer.jupiter.cartesian.*;
70-
import org.junitpioneer.jupiter.params.*;
71-
import org.junitpioneer.jupiter.cartesian.CartesianTest.Values;
72-
import org.junitpioneer.jupiter.cartesian.CartesianTest.Enum;
73-
7465
include::{demo}[tag=cartesian_annotating_parameters]
7566
----
7667

@@ -81,9 +72,6 @@ The test will try every combination those values can have.
8172

8273
[source,java,indent=0]
8374
----
84-
import org.junitpioneer.jupiter.cartesian.*;
85-
import org.junitpioneer.jupiter.cartesian.CartesianTest.Values;
86-
8775
include::{demo}[tag=cartesian_combining_values]
8876
----
8977

@@ -113,11 +101,6 @@ To demonstrate with a table:
113101

114102
[source,java,indent=0]
115103
----
116-
import java.time.temporal.ChronoUnit;
117-
118-
import org.junitpioneer.jupiter.cartesian.*;
119-
import org.junitpioneer.jupiter.cartesian.CartesianTest.Enum;
120-
121104
include::{demo}[tag=cartesian_with_enum]
122105
----
123106

@@ -131,11 +114,6 @@ The `value` attribute is required in the following example because the method pa
131114

132115
[source,java,indent=0]
133116
----
134-
import java.time.temporal.*;
135-
136-
import org.junitpioneer.jupiter.cartesian.*;
137-
import org.junitpioneer.jupiter.cartesian.CartesianTest.Enum;
138-
139117
include::{demo}[tag=cartesian_enum_with_type]
140118
----
141119

@@ -144,11 +122,6 @@ If omitted, all constants will be used.
144122

145123
[source,java,indent=0]
146124
----
147-
import java.time.temporal.*;
148-
149-
import org.junitpioneer.jupiter.cartesian.*;
150-
import org.junitpioneer.jupiter.cartesian.CartesianTest.Enum;
151-
152125
include::{demo}[tag=cartesian_enum_with_type_and_names]
153126
----
154127

@@ -157,31 +130,18 @@ For example, you can exclude names from the enum constant pool or specify regula
157130

158131
[source,java,indent=0]
159132
----
160-
import java.time.temporal.*;
161-
162-
import org.junitpioneer.jupiter.cartesian.*;
163-
import org.junitpioneer.jupiter.cartesian.CartesianTest.Enum;
164-
165133
include::{demo}[tag=cartesian_enum_with_mode]
166134
----
167135

168136
[source,java,indent=0]
169137
----
170-
import java.time.temporal.*;
171-
172-
import org.junitpioneer.jupiter.cartesian.*;
173-
import org.junitpioneer.jupiter.cartesian.CartesianTest.Enum;
174-
175138
include::{demo}[tag=cartesian_enum_with_regex]
176139
----
177140

178141
The example below shows how to use `@CartesianTest.Enum` with two `Enum` types.
179142

180143
[source,java,indent=0]
181144
----
182-
import org.junitpioneer.jupiter.cartesian.*;
183-
import org.junitpioneer.jupiter.cartesian.CartesianTest.Enum;
184-
185145
include::{demo}[tag=cartesian_enum_with_enums]
186146
----
187147

@@ -202,16 +162,13 @@ To demonstrate with a table:
202162
| 6th test | THREE | GAMMA
203163
|===
204164

205-
=== Range Source annotations
165+
=== Range source annotations
206166

207167
You can annotate your test parameters with link:docs/range-sources[range source annotations].
208168
For _this purpose only_, range sources can be used on parameters.
209169

210170
[source,java,indent=0]
211171
----
212-
import org.junitpioneer.jupiter.cartesian.*;
213-
import org.junitpioneer.jupiter.params.*;
214-
215172
include::{demo}[tag=cartesian_enum_with_range_sources]
216173
----
217174

@@ -233,7 +190,7 @@ To demonstrate with a table:
233190

234191
For more information, please see the link:docs/range-sources[separate documentation about range sources].
235192

236-
== Annotating your test method
193+
== Defining arguments with factories
237194

238195
You can annotate your test method to supply arguments to all parameters simultaneously.
239196

@@ -245,9 +202,9 @@ Just like with JUnit's `@MethodSource`, you can specify the factory method with
245202
This method must return `ArgumentSets`.
246203

247204
`ArgumentSets` is a helper class, specifically for creating sets for `@CartesianTest`.
248-
To create the test data, instantiate with the static factory method `argumentsForFirstParameter`, then call the `addValuesForNextParameter` method once per additional parameter in the order in which the they appear in the test method.
205+
To create the test data, instantiate with the static factory method `argumentsForFirstParameter`, then call the `addValuesForNextParameter` method once per additional parameter in the order in which they appear in the test method.
249206
In each call, pass in all values for the corresponding parameter.
250-
For convenience, all methods return with your `ArgumentSets` instance, so you can chain `add()` calls.
207+
For convenience, all methods return with your `ArgumentSets` instance, so you can chain `add...` calls.
251208
If you want to create an initially-empty `ArgumentSets`, call the static factory method `create()`.
252209

253210
Let's look at an example.
@@ -288,11 +245,11 @@ You can reuse the same argument provider method multiple times.
288245
include::{demo}[tag=cartesian_argument_sets_reuse]
289246
----
290247

291-
You can use an argument provider method in nested classes, as long as these are annotated with `@TestInstance(Lifecycle.PER_CLASS)`.
248+
You can make the argument provider method non-static if the test class is annotated with `@TestInstance(Lifecycle.PER_CLASS)`.
292249

293250
[source,java,indent=0]
294251
----
295-
include::{demo}[tag=cartesian_argument_sets_with_nested_classes]
252+
include::{demo}[tag=cartesian_argument_sets_with_non_static_factory]
296253
----
297254

298255
==== Requirements for the factory method
@@ -341,10 +298,7 @@ Let's create an annotation for it.
341298

342299
[source,java,indent=0]
343300
----
344-
import java.lang.annotation.*;
345-
import org.junitpioneer.jupiter.cartesian.CartesianArgumentsSource;
346-
347-
include::{demo}[tag=cartesian_argument_sets_int_argument_provider]
301+
include::{demo}[tag=cartesian_argument_sets_ints_annotation]
348302
----
349303

350304
The annotation targets parameters because we want to use it directly on a parameter.
@@ -355,15 +309,7 @@ Next, we need to implement `IntArgumentsProvider`, that takes these values and p
355309

356310
[source,java,indent=0]
357311
----
358-
import java.lang.reflect.Parameter;
359-
import java.util.Arrays;
360-
import java.util.Objects;
361-
import java.util.stream.Stream;
362-
363-
import org.junit.jupiter.api.extension.ExtensionContext;
364-
import org.junitpioneer.jupiter.cartesian.CartesianParameterArgumentsProvider;
365-
366-
include::{demo}[tag=cartesian_argument_sets_ints_annotation]
312+
include::{demo}[tag=cartesian_argument_sets_int_argument_provider]
367313
----
368314

369315
The class has to implement `CartesianParameterArgumentsProvider`.
@@ -376,14 +322,6 @@ You could do additional processing, for example:
376322

377323
[source,java,indent=0]
378324
----
379-
import java.lang.annotation.*;
380-
import java.util.*;
381-
import java.lang.reflect.Parameter;
382-
383-
import org.junit.jupiter.api.extension.ExtensionContext;
384-
import org.junitpioneer.jupiter.cartesian.CartesianArgumentsSource;
385-
import org.junitpioneer.jupiter.cartesian.CartesianParameterArgumentsProvider;
386-
387325
include::{people}[tag=cartesian_people_provider_with_CartesianParameterArgumentsProvider]
388326
----
389327

@@ -392,15 +330,6 @@ The previous example would look like the following:
392330

393331
[source,java,indent=0]
394332
----
395-
import java.lang.annotation.*;
396-
import java.util.*;
397-
import java.lang.reflect.Parameter;
398-
399-
import org.junit.jupiter.api.extension.ExtensionContext;
400-
import org.junit.jupiter.params.support.AnnotationConsumer;
401-
import org.junitpioneer.jupiter.cartesian.CartesianArgumentsSource;
402-
import org.junitpioneer.jupiter.cartesian.CartesianParameterArgumentsProvider;
403-
404333
include::{people}[tag=cartesian_people_provider_with_AnnotationConsumer]
405334
----
406335

@@ -416,9 +345,6 @@ Let's create an annotation for it.
416345

417346
[source,java,indent=0]
418347
----
419-
import java.lang.annotation.*;
420-
import org.junitpioneer.jupiter.cartesian.CartesianArgumentsSource;
421-
422348
include::{bit}[tag=cartesian_bit_source_annotation]
423349
----
424350

@@ -430,14 +356,6 @@ Next, we need to implement `BitArgumentsProvider`.
430356

431357
[source,java,indent=0]
432358
----
433-
import java.lang.reflect.Parameter;
434-
import java.util.Arrays;
435-
import java.util.Objects;
436-
import java.util.stream.Stream;
437-
438-
import org.junit.jupiter.api.extension.ExtensionContext;
439-
import org.junitpioneer.jupiter.cartesian.CartesianMethodArgumentsProvider;
440-
441359
include::{bit}[tag=cartesian_bit_argument_provider]
442360
----
443361

@@ -454,16 +372,6 @@ For example:
454372

455373
[source,java,indent=0]
456374
----
457-
import java.lang.annotation.*;
458-
import java.lang.reflect.Parameter;
459-
import java.util.Arrays;
460-
import java.util.Objects;
461-
import java.util.stream.Stream;
462-
463-
import org.junit.jupiter.api.extension.ExtensionContext;
464-
import org.junitpioneer.jupiter.cartesian.CartesianArgumentsSource;
465-
import org.junitpioneer.jupiter.cartesian.CartesianMethodArgumentsProvider;
466-
467375
include::{number}[tag=cartesian_number_argument_provider]
468376
----
469377

@@ -475,9 +383,6 @@ For example:
475383

476384
[source,java,indent=0]
477385
----
478-
import org.junitpioneer.jupiter.cartesian.*;
479-
import org.junitpioneer.jupiter.cartesian.CartesianTest.Values;
480-
481386
include::{demo}[tag=cartesian_testWithCustomDisplayName]
482387
----
483388

0 commit comments

Comments
 (0)