Skip to content

Commit 890073b

Browse files
committed
Extract MyRandomParametersTest to separate files and use local extension
Use local RandomNumberExtension instead of referencing external RandomParametersExtension from junit-examples repository. Demonstrate constructor and method parameter injection. Signed-off-by: Anton.Yalyshev <[email protected]>
1 parent 9e495dd commit 890073b

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

documentation/modules/ROOT/pages/writing-tests/dependency-injection-for-constructors-and-methods.adoc

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,7 @@ Java::
102102
--
103103
[source,java,indent=0]
104104
----
105-
@ExtendWith(RandomParametersExtension.class)
106-
class MyRandomParametersTest {
107-
108-
@Test
109-
void injectsInteger(@Random int i, @Random int j) {
110-
assertNotEquals(i, j);
111-
}
112-
113-
@Test
114-
void injectsDouble(@Random double d) {
115-
assertEquals(0.0, d, 1.0);
116-
}
117-
118-
}
105+
include::example$java/example/MyRandomParametersTest.java[tags=user_guide]
119106
----
120107
--
121108
@@ -124,19 +111,7 @@ Kotlin::
124111
--
125112
[source,kotlin,indent=0]
126113
----
127-
@ExtendWith(RandomParametersExtension::class)
128-
class KotlinMyRandomParametersTest {
129-
130-
@Test
131-
fun injectsInteger(@Random i: Int, @Random j: Int) {
132-
assertNotEquals(i, j)
133-
}
134-
135-
@Test
136-
fun injectsDouble(@Random d: Double) {
137-
assertEquals(0.0, d, 1.0)
138-
}
139-
}
114+
include::example$kotlin/example/kotlin/MyRandomParametersTest.kt[tags=user_guide]
140115
----
141116
--
142117
====
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2015-2026 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package example;
12+
13+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
14+
15+
import example.extensions.Random;
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
// tag::user_guide[]
20+
class MyRandomParametersTest {
21+
22+
private final int randomNumber;
23+
24+
MyRandomParametersTest(@Random int randomNumber) {
25+
this.randomNumber = randomNumber;
26+
}
27+
28+
@Test
29+
void injectsInteger(@Random int i, @Random int j) {
30+
assertNotEquals(i, j);
31+
}
32+
33+
}
34+
// end::user_guide[]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2015-2026 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package example.kotlin
12+
13+
// tag::user_guide[]
14+
import example.extensions.Random
15+
import org.junit.jupiter.api.Assertions.assertNotEquals
16+
import org.junit.jupiter.api.Test
17+
18+
class MyRandomParametersTest(
19+
@Random private val randomNumber: Int
20+
) {
21+
@Test
22+
fun injectsInteger(
23+
@Random i: Int,
24+
@Random j: Int
25+
) {
26+
assertNotEquals(i, j)
27+
}
28+
}
29+
// end::user_guide[]

0 commit comments

Comments
 (0)