Skip to content

Commit a5eaffe

Browse files
committed
Move Micrometer related tests to dedicated test classes
1 parent e4d2523 commit a5eaffe

File tree

4 files changed

+109
-148
lines changed

4 files changed

+109
-148
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/observability/micrometer/MicrometerMetrics.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ private MicrometerMetrics() {
4141
/**
4242
* Create a new {@link Observation}. It's not started, you must explicitly call
4343
* {@link Observation#start()} to start it.
44-
* <p>
4544
* @param name of the observation
4645
* @param observationRegistry the observation registry to use
4746
* @return a new observation instance

spring-batch-core/src/test/java/org/springframework/batch/core/observability/BatchMetricsTests.java

Lines changed: 0 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,9 @@
1818
import java.time.Duration;
1919
import java.time.LocalDateTime;
2020
import java.time.temporal.ChronoUnit;
21-
import java.util.Arrays;
22-
import java.util.List;
2321

24-
import javax.sql.DataSource;
25-
26-
import io.micrometer.core.instrument.Meter;
27-
import io.micrometer.core.instrument.MeterRegistry;
28-
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
29-
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
30-
import io.micrometer.observation.ObservationRegistry;
3122
import org.junit.jupiter.api.Test;
3223

33-
import org.springframework.batch.core.ExitStatus;
34-
import org.springframework.batch.core.configuration.annotation.EnableJdbcJobRepository;
35-
import org.springframework.batch.core.job.Job;
36-
import org.springframework.batch.core.job.JobExecution;
37-
import org.springframework.batch.core.job.parameters.JobParameters;
38-
import org.springframework.batch.core.step.Step;
39-
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
40-
import org.springframework.batch.core.job.builder.JobBuilder;
41-
import org.springframework.batch.core.launch.JobOperator;
42-
import org.springframework.batch.core.repository.JobRepository;
43-
import org.springframework.batch.core.step.builder.StepBuilder;
44-
import org.springframework.batch.item.support.ListItemReader;
45-
import org.springframework.context.ApplicationContext;
46-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
47-
import org.springframework.context.annotation.Bean;
48-
import org.springframework.context.annotation.Configuration;
49-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
50-
import org.springframework.jdbc.support.JdbcTransactionManager;
51-
import org.springframework.transaction.PlatformTransactionManager;
52-
53-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
5424
import static org.junit.jupiter.api.Assertions.assertEquals;
5525
import static org.junit.jupiter.api.Assertions.assertNull;
5626
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -132,118 +102,4 @@ void testFormatNullDuration() {
132102
assertTrue(formattedDuration.isEmpty());
133103
}
134104

135-
@Test
136-
void testBatchMetrics() throws Exception {
137-
// given
138-
ApplicationContext context = new AnnotationConfigApplicationContext(MyJobConfiguration.class);
139-
JobOperator jobOperator = context.getBean(JobOperator.class);
140-
Job job = context.getBean(Job.class);
141-
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
142-
int expectedBatchMetricsCount = 6;
143-
144-
// when
145-
JobExecution jobExecution = jobOperator.start(job, new JobParameters());
146-
147-
// then
148-
assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
149-
List<Meter> meters = meterRegistry.getMeters();
150-
assertTrue(meters.size() >= expectedBatchMetricsCount);
151-
152-
// Job metrics
153-
154-
assertDoesNotThrow(() -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + "job.launch.count").timer(),
155-
"There should be a meter of type TIMER named spring.batch.job.launch.count registered in the meter registry");
156-
assertEquals(1, meterRegistry.get(BatchMetrics.METRICS_PREFIX + "job.launch.count").timer().count());
157-
158-
assertDoesNotThrow(
159-
() -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + "job")
160-
.tag(BatchMetrics.METRICS_PREFIX + "job.name", "job")
161-
.tag(BatchMetrics.METRICS_PREFIX + "job.status", "COMPLETED")
162-
.timer(),
163-
"There should be a meter of type TIMER named spring.batch.job registered in the meter registry");
164-
165-
// Step metrics
166-
167-
assertDoesNotThrow(
168-
() -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + "step")
169-
.tag(BatchMetrics.METRICS_PREFIX + "step.name", "step")
170-
.tag(BatchMetrics.METRICS_PREFIX + "step.job.name", "job")
171-
.tag(BatchMetrics.METRICS_PREFIX + "step.status", "COMPLETED")
172-
.timer(),
173-
"There should be a meter of type TIMER named spring.batch.step registered in the meter registry");
174-
175-
assertDoesNotThrow(
176-
() -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + ".item.read")
177-
.tag(BatchMetrics.METRICS_PREFIX + ".item.read.job.name", "job")
178-
.tag(BatchMetrics.METRICS_PREFIX + ".item.read.step.name", "step")
179-
.tag(BatchMetrics.METRICS_PREFIX + ".item.read.status", "SUCCESS")
180-
.timer(),
181-
"There should be a meter of type TIMER named spring.batch.item.read registered in the meter registry");
182-
183-
assertDoesNotThrow(
184-
() -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + "item.process")
185-
.tag(BatchMetrics.METRICS_PREFIX + "item.process.job.name", "job")
186-
.tag(BatchMetrics.METRICS_PREFIX + "item.process.step.name", "step")
187-
.tag(BatchMetrics.METRICS_PREFIX + "item.process.status", "SUCCESS")
188-
.timer(),
189-
"There should be a meter of type TIMER named spring.batch.item.process registered in the meter registry");
190-
191-
assertDoesNotThrow(
192-
() -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + "chunk.write")
193-
.tag(BatchMetrics.METRICS_PREFIX + "chunk.write.job.name", "job")
194-
.tag(BatchMetrics.METRICS_PREFIX + "chunk.write.step.name", "step")
195-
.tag(BatchMetrics.METRICS_PREFIX + "chunk.write.status", "SUCCESS")
196-
.timer(),
197-
"There should be a meter of type TIMER named spring.batch.chunk.write registered in the meter registry");
198-
199-
}
200-
201-
@Configuration
202-
@EnableBatchProcessing
203-
@EnableJdbcJobRepository
204-
static class MyJobConfiguration {
205-
206-
@Bean
207-
public Step step(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
208-
return new StepBuilder(jobRepository).<Integer, Integer>chunk(2)
209-
.transactionManager(transactionManager)
210-
.reader(new ListItemReader<>(Arrays.asList(1, 2, 3, 4, 5)))
211-
.writer(items -> {
212-
})
213-
.build();
214-
}
215-
216-
@Bean
217-
public Job job(JobRepository jobRepository, Step step) {
218-
return new JobBuilder("job", jobRepository).start(step).build();
219-
}
220-
221-
@Bean
222-
public MeterRegistry meterRegistry() {
223-
return new SimpleMeterRegistry();
224-
}
225-
226-
@Bean
227-
public ObservationRegistry observationRegistry(MeterRegistry meterRegistry) {
228-
ObservationRegistry observationRegistry = ObservationRegistry.create();
229-
observationRegistry.observationConfig()
230-
.observationHandler(new DefaultMeterObservationHandler(meterRegistry));
231-
return observationRegistry;
232-
}
233-
234-
@Bean
235-
public DataSource dataSource() {
236-
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
237-
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
238-
.generateUniqueName(true)
239-
.build();
240-
}
241-
242-
@Bean
243-
public JdbcTransactionManager transactionManager(DataSource dataSource) {
244-
return new JdbcTransactionManager(dataSource);
245-
}
246-
247-
}
248-
249105
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2025-present the original author or 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 org.springframework.batch.core.observability.micrometer;
17+
18+
import java.util.List;
19+
20+
import io.micrometer.core.instrument.Meter;
21+
import io.micrometer.core.instrument.MeterRegistry;
22+
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
23+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
24+
import io.micrometer.observation.ObservationRegistry;
25+
import org.junit.jupiter.api.Test;
26+
27+
import org.springframework.batch.core.ExitStatus;
28+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
29+
import org.springframework.batch.core.job.Job;
30+
import org.springframework.batch.core.job.JobExecution;
31+
import org.springframework.batch.core.job.builder.JobBuilder;
32+
import org.springframework.batch.core.job.parameters.JobParameters;
33+
import org.springframework.batch.core.launch.JobOperator;
34+
import org.springframework.batch.core.observability.BatchMetrics;
35+
import org.springframework.batch.core.repository.JobRepository;
36+
import org.springframework.batch.core.step.builder.StepBuilder;
37+
import org.springframework.batch.repeat.RepeatStatus;
38+
import org.springframework.context.ApplicationContext;
39+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
40+
import org.springframework.context.annotation.Bean;
41+
import org.springframework.context.annotation.Configuration;
42+
43+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
44+
import static org.junit.jupiter.api.Assertions.assertEquals;
45+
import static org.junit.jupiter.api.Assertions.assertTrue;
46+
47+
class MicrometerMetricsTests {
48+
49+
@Test
50+
void testMicrometerMetrics() throws Exception {
51+
// given
52+
ApplicationContext context = new AnnotationConfigApplicationContext(MyJobConfiguration.class);
53+
JobOperator jobOperator = context.getBean(JobOperator.class);
54+
Job job = context.getBean(Job.class);
55+
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
56+
int expectedJobMetricsCount = 2;
57+
58+
// when
59+
JobExecution jobExecution = jobOperator.start(job, new JobParameters());
60+
61+
// then
62+
assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
63+
List<Meter> meters = meterRegistry.getMeters();
64+
assertTrue(meters.size() >= expectedJobMetricsCount);
65+
assertDoesNotThrow(() -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + "job.launch.count").timer(),
66+
"There should be a meter of type TIMER named spring.batch.job.launch.count registered in the meter registry");
67+
assertEquals(1, meterRegistry.get(BatchMetrics.METRICS_PREFIX + "job.launch.count").timer().count());
68+
assertDoesNotThrow(
69+
() -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + "job")
70+
.tag(BatchMetrics.METRICS_PREFIX + "job.name", "job")
71+
.tag(BatchMetrics.METRICS_PREFIX + "job.status", "COMPLETED")
72+
.timer(),
73+
"There should be a meter of type TIMER named spring.batch.job registered in the meter registry");
74+
75+
}
76+
77+
@Configuration
78+
@EnableBatchProcessing
79+
static class MyJobConfiguration {
80+
81+
@Bean
82+
public Job job(JobRepository jobRepository) {
83+
return new JobBuilder("job", jobRepository)
84+
.start(new StepBuilder("step", jobRepository)
85+
.tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED)
86+
.build())
87+
.build();
88+
}
89+
90+
@Bean
91+
public MeterRegistry meterRegistry() {
92+
return new SimpleMeterRegistry();
93+
}
94+
95+
@Bean
96+
public ObservationRegistry observationRegistry(MeterRegistry meterRegistry) {
97+
ObservationRegistry observationRegistry = ObservationRegistry.create();
98+
observationRegistry.observationConfig()
99+
.observationHandler(new DefaultMeterObservationHandler(meterRegistry));
100+
return observationRegistry;
101+
}
102+
103+
}
104+
105+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.batch.core.observability;
16+
package org.springframework.batch.core.observability.micrometer;
1717

1818
import java.util.UUID;
1919

@@ -33,6 +33,7 @@
3333
import org.springframework.batch.core.job.JobExecution;
3434
import org.springframework.batch.core.job.parameters.JobParameters;
3535
import org.springframework.batch.core.job.parameters.JobParametersBuilder;
36+
import org.springframework.batch.core.observability.BatchMetrics;
3637
import org.springframework.batch.core.step.Step;
3738
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
3839
import org.springframework.batch.core.job.builder.JobBuilder;
@@ -46,7 +47,7 @@
4647
import org.springframework.test.context.junit.jupiter.SpringExtension;
4748

4849
@ExtendWith(SpringExtension.class)
49-
class ObservabilitySampleStepTests extends SampleTestRunner {
50+
class MicrometerTracingTests extends SampleTestRunner {
5051

5152
@Autowired
5253
private Job job;
@@ -60,7 +61,7 @@ class ObservabilitySampleStepTests extends SampleTestRunner {
6061
@Autowired
6162
private ObservationRegistry observationRegistry;
6263

63-
ObservabilitySampleStepTests() {
64+
MicrometerTracingTests() {
6465
super(SampleRunnerConfig.builder().build());
6566
}
6667

0 commit comments

Comments
 (0)