File tree Expand file tree Collapse file tree
operator-framework/src/test/java/io/javaoperatorsdk/operator
externalstatebulkdependent Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1515 */
1616package io .javaoperatorsdk .operator .dependent .externalstate ;
1717
18- import org .junit .jupiter .api .BeforeEach ;
1918import org .junit .jupiter .api .Test ;
19+ import org .junit .jupiter .api .extension .ExtendWith ;
2020
2121import io .fabric8 .kubernetes .api .model .ConfigMap ;
2222import io .fabric8 .kubernetes .api .model .ObjectMetaBuilder ;
2323import io .javaoperatorsdk .operator .junit .LocallyRunOperatorExtension ;
2424import io .javaoperatorsdk .operator .support .ExternalIDGenServiceMock ;
25+ import io .javaoperatorsdk .operator .support .ExternalServiceResetExtension ;
2526
2627import static io .javaoperatorsdk .operator .dependent .externalstate .ExternalStateReconciler .ID_KEY ;
2728import static org .assertj .core .api .Assertions .assertThat ;
2829import static org .awaitility .Awaitility .await ;
2930
31+ @ ExtendWith (ExternalServiceResetExtension .class )
3032public abstract class ExternalStateTestBase {
3133
3234 private static final String TEST_RESOURCE_NAME = "test1" ;
@@ -36,11 +38,6 @@ public abstract class ExternalStateTestBase {
3638
3739 private final ExternalIDGenServiceMock externalService = ExternalIDGenServiceMock .getInstance ();
3840
39- @ BeforeEach
40- void resetExternalService () {
41- externalService .reset ();
42- }
43-
4441 @ Test
4542 public void reconcilesResourceWithPersistentState () {
4643 var resource = extension ().create (testResource ());
Original file line number Diff line number Diff line change 1717
1818import java .time .Duration ;
1919
20- import org .junit .jupiter .api .BeforeEach ;
2120import org .junit .jupiter .api .Test ;
21+ import org .junit .jupiter .api .extension .ExtendWith ;
2222import org .junit .jupiter .api .extension .RegisterExtension ;
2323
2424import io .fabric8 .kubernetes .api .model .ObjectMetaBuilder ;
2525import io .javaoperatorsdk .annotation .Sample ;
2626import io .javaoperatorsdk .operator .junit .LocallyRunOperatorExtension ;
2727import io .javaoperatorsdk .operator .support .ExternalIDGenServiceMock ;
28+ import io .javaoperatorsdk .operator .support .ExternalServiceResetExtension ;
2829
2930import static org .assertj .core .api .Assertions .assertThat ;
3031import static org .awaitility .Awaitility .await ;
3839 allowing operators to track and reconcile a variable number of external resources with \
3940 persistent state that survives operator restarts.
4041 """ )
42+ @ ExtendWith (ExternalServiceResetExtension .class )
4143class ExternalStateBulkIT {
4244
4345 private static final String TEST_RESOURCE_NAME = "test1" ;
@@ -50,11 +52,6 @@ class ExternalStateBulkIT {
5052
5153 private final ExternalIDGenServiceMock externalService = ExternalIDGenServiceMock .getInstance ();
5254
53- @ BeforeEach
54- void resetExternalService () {
55- externalService .reset ();
56- }
57-
5855 @ RegisterExtension
5956 LocallyRunOperatorExtension operator =
6057 LocallyRunOperatorExtension .builder ()
Original file line number Diff line number Diff line change @@ -51,8 +51,8 @@ public List<ExternalResource> listResources() {
5151 }
5252
5353 /**
54- * Clears the internal state. Intended to be called between tests so that state from one test
55- * does not leak into the next via this JVM-lifetime singleton.
54+ * Clears the internal state. Intended to be called between tests so that state from one test does
55+ * not leak into the next via this JVM-lifetime singleton.
5656 */
5757 public void reset () {
5858 resourceMap .clear ();
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright Java Operator SDK Authors
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ package io .javaoperatorsdk .operator .support ;
17+
18+ import org .jspecify .annotations .NonNull ;
19+ import org .junit .jupiter .api .extension .BeforeEachCallback ;
20+ import org .junit .jupiter .api .extension .ExtensionContext ;
21+
22+ /**
23+ * Resets the {@link ExternalIDGenServiceMock} singleton before each test method so that state from
24+ * one test does not leak into the next. Apply via
25+ * {@code @ExtendWith(ExternalServiceResetExtension.class)}.
26+ */
27+ public class ExternalServiceResetExtension implements BeforeEachCallback {
28+
29+ @ Override
30+ public void beforeEach (@ NonNull ExtensionContext context ) {
31+ ExternalIDGenServiceMock .getInstance ().reset ();
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments