Step Testing in Spring Batch
while testing a step which is a chunk oriented processing that contains reader and writer using jobLauncherTestUtils.launchstep().
1.how to pass or set the job explorer while testing it as a single step?
Step:
@Bean(name="step_name")
public step step1(RepositoryItemReader<Person> perosnItemReader,
FlatFileItemWriter<String> personItemWriter,
TaskExecutor personTaskExecutor){
return StepBuilderFactory
.get("step_name")
.listener(new listener())
.<Person,String>Chunk(200)
.reader(personItemReader)
.writer(personItemWriter)
.listener(new DateReadListener())
.build();
}
Reader:
@Bean(name="step_reader")
public RepositoryItemReader<Person> personItemReader(
@Value("#jobParameters[id]}" String id){
return new RepositoryItemReader<Person>()
.name("step_reader")
.repository(personDao)
.methodName("getDetails")
.arguments(id)
.build();
}
Writer:
@Bean(name="step_writer")
public FlatFileItemWriter<String> personItemWriter(
@Value("#{jobExecutionContext[person]}") Person person,
@Value("#{stepExecution.jobExecution.id}" long id,
JobExplorer jobExplorer) {
Foo foo = new
Foo(jobExplorer.getJobExecution(jobId).getExecutionContext(),new
CurrentDate());
return new FlatFileItemWriterBuilder()
.get("item_writer")
.callback(foo)
.build();
}
The Way i Tested It.
ExecutionContext executionContext = new ExecutionContext()
executionContext.put("person",Person);
JobExecution jobExecution =
JobLauncherTestUtils.launchStep("step_name",jobparams,executionContext);
Issue
class Foo{
Foo f = new Foo(JobExecution jobExection, Date date);
Person person = (Person)jobExecution.getExecutionContext().get("person");
}
Here,jobExecution returning null value.
ThanksInAdavnce
spring-batch batch-processing
add a comment |
while testing a step which is a chunk oriented processing that contains reader and writer using jobLauncherTestUtils.launchstep().
1.how to pass or set the job explorer while testing it as a single step?
Step:
@Bean(name="step_name")
public step step1(RepositoryItemReader<Person> perosnItemReader,
FlatFileItemWriter<String> personItemWriter,
TaskExecutor personTaskExecutor){
return StepBuilderFactory
.get("step_name")
.listener(new listener())
.<Person,String>Chunk(200)
.reader(personItemReader)
.writer(personItemWriter)
.listener(new DateReadListener())
.build();
}
Reader:
@Bean(name="step_reader")
public RepositoryItemReader<Person> personItemReader(
@Value("#jobParameters[id]}" String id){
return new RepositoryItemReader<Person>()
.name("step_reader")
.repository(personDao)
.methodName("getDetails")
.arguments(id)
.build();
}
Writer:
@Bean(name="step_writer")
public FlatFileItemWriter<String> personItemWriter(
@Value("#{jobExecutionContext[person]}") Person person,
@Value("#{stepExecution.jobExecution.id}" long id,
JobExplorer jobExplorer) {
Foo foo = new
Foo(jobExplorer.getJobExecution(jobId).getExecutionContext(),new
CurrentDate());
return new FlatFileItemWriterBuilder()
.get("item_writer")
.callback(foo)
.build();
}
The Way i Tested It.
ExecutionContext executionContext = new ExecutionContext()
executionContext.put("person",Person);
JobExecution jobExecution =
JobLauncherTestUtils.launchStep("step_name",jobparams,executionContext);
Issue
class Foo{
Foo f = new Foo(JobExecution jobExection, Date date);
Person person = (Person)jobExecution.getExecutionContext().get("person");
}
Here,jobExecution returning null value.
ThanksInAdavnce
spring-batch batch-processing
add a comment |
while testing a step which is a chunk oriented processing that contains reader and writer using jobLauncherTestUtils.launchstep().
1.how to pass or set the job explorer while testing it as a single step?
Step:
@Bean(name="step_name")
public step step1(RepositoryItemReader<Person> perosnItemReader,
FlatFileItemWriter<String> personItemWriter,
TaskExecutor personTaskExecutor){
return StepBuilderFactory
.get("step_name")
.listener(new listener())
.<Person,String>Chunk(200)
.reader(personItemReader)
.writer(personItemWriter)
.listener(new DateReadListener())
.build();
}
Reader:
@Bean(name="step_reader")
public RepositoryItemReader<Person> personItemReader(
@Value("#jobParameters[id]}" String id){
return new RepositoryItemReader<Person>()
.name("step_reader")
.repository(personDao)
.methodName("getDetails")
.arguments(id)
.build();
}
Writer:
@Bean(name="step_writer")
public FlatFileItemWriter<String> personItemWriter(
@Value("#{jobExecutionContext[person]}") Person person,
@Value("#{stepExecution.jobExecution.id}" long id,
JobExplorer jobExplorer) {
Foo foo = new
Foo(jobExplorer.getJobExecution(jobId).getExecutionContext(),new
CurrentDate());
return new FlatFileItemWriterBuilder()
.get("item_writer")
.callback(foo)
.build();
}
The Way i Tested It.
ExecutionContext executionContext = new ExecutionContext()
executionContext.put("person",Person);
JobExecution jobExecution =
JobLauncherTestUtils.launchStep("step_name",jobparams,executionContext);
Issue
class Foo{
Foo f = new Foo(JobExecution jobExection, Date date);
Person person = (Person)jobExecution.getExecutionContext().get("person");
}
Here,jobExecution returning null value.
ThanksInAdavnce
spring-batch batch-processing
while testing a step which is a chunk oriented processing that contains reader and writer using jobLauncherTestUtils.launchstep().
1.how to pass or set the job explorer while testing it as a single step?
Step:
@Bean(name="step_name")
public step step1(RepositoryItemReader<Person> perosnItemReader,
FlatFileItemWriter<String> personItemWriter,
TaskExecutor personTaskExecutor){
return StepBuilderFactory
.get("step_name")
.listener(new listener())
.<Person,String>Chunk(200)
.reader(personItemReader)
.writer(personItemWriter)
.listener(new DateReadListener())
.build();
}
Reader:
@Bean(name="step_reader")
public RepositoryItemReader<Person> personItemReader(
@Value("#jobParameters[id]}" String id){
return new RepositoryItemReader<Person>()
.name("step_reader")
.repository(personDao)
.methodName("getDetails")
.arguments(id)
.build();
}
Writer:
@Bean(name="step_writer")
public FlatFileItemWriter<String> personItemWriter(
@Value("#{jobExecutionContext[person]}") Person person,
@Value("#{stepExecution.jobExecution.id}" long id,
JobExplorer jobExplorer) {
Foo foo = new
Foo(jobExplorer.getJobExecution(jobId).getExecutionContext(),new
CurrentDate());
return new FlatFileItemWriterBuilder()
.get("item_writer")
.callback(foo)
.build();
}
The Way i Tested It.
ExecutionContext executionContext = new ExecutionContext()
executionContext.put("person",Person);
JobExecution jobExecution =
JobLauncherTestUtils.launchStep("step_name",jobparams,executionContext);
Issue
class Foo{
Foo f = new Foo(JobExecution jobExection, Date date);
Person person = (Person)jobExecution.getExecutionContext().get("person");
}
Here,jobExecution returning null value.
ThanksInAdavnce
spring-batch batch-processing
spring-batch batch-processing
edited Jan 2 at 16:59
Hanlak
asked Dec 26 '18 at 19:36
HanlakHanlak
15
15
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
How can i check wheather reader returns the data successfully?
you don't need to test the whole step to check if the reader returns the data correctly. A unit test of the reader itself should be enough: create and open the reader, call read
and assert on the result.
2.how to pass job execution id and job explorer to writer which takes both values as parameters?
Why would an item writer need a job execution and a job explorer? From your code, those are not used when creating the writer.
The step i am supposed to test contains the above reader and writer, i am testing that step with jobLauncherTestUtils.launchStep(stepname,<jobParam>,<executioncontext>). when it goes into writer the jobexplorer is being used to fetch the data from execution context.It works fine when testing as a whole job but not as a single step.
– Hanlak
Dec 29 '18 at 5:34
if your test context is managed by Spring (which I am expecting), then you can inject the job explorer and pass it to the writer.
– Mahmoud Ben Hassine
Dec 29 '18 at 21:52
Actually,I am a newbie to spring and spring batch it would be so helpful for me if you give an example or any link to a sample code.Thanks In Advance
– Hanlak
Dec 30 '18 at 5:56
you can find code examples of how to test a single step or a step-scoped component within a step in this section: docs.spring.io/spring-batch/4.1.x/reference/html/…
– Mahmoud Ben Hassine
Dec 30 '18 at 9:45
I tried what is shown in documentation but i think it still require some add on to add, but i don`t know what is it.could you please help me
– Hanlak
Jan 2 at 17:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53936364%2fstep-testing-in-spring-batch%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
How can i check wheather reader returns the data successfully?
you don't need to test the whole step to check if the reader returns the data correctly. A unit test of the reader itself should be enough: create and open the reader, call read
and assert on the result.
2.how to pass job execution id and job explorer to writer which takes both values as parameters?
Why would an item writer need a job execution and a job explorer? From your code, those are not used when creating the writer.
The step i am supposed to test contains the above reader and writer, i am testing that step with jobLauncherTestUtils.launchStep(stepname,<jobParam>,<executioncontext>). when it goes into writer the jobexplorer is being used to fetch the data from execution context.It works fine when testing as a whole job but not as a single step.
– Hanlak
Dec 29 '18 at 5:34
if your test context is managed by Spring (which I am expecting), then you can inject the job explorer and pass it to the writer.
– Mahmoud Ben Hassine
Dec 29 '18 at 21:52
Actually,I am a newbie to spring and spring batch it would be so helpful for me if you give an example or any link to a sample code.Thanks In Advance
– Hanlak
Dec 30 '18 at 5:56
you can find code examples of how to test a single step or a step-scoped component within a step in this section: docs.spring.io/spring-batch/4.1.x/reference/html/…
– Mahmoud Ben Hassine
Dec 30 '18 at 9:45
I tried what is shown in documentation but i think it still require some add on to add, but i don`t know what is it.could you please help me
– Hanlak
Jan 2 at 17:00
add a comment |
How can i check wheather reader returns the data successfully?
you don't need to test the whole step to check if the reader returns the data correctly. A unit test of the reader itself should be enough: create and open the reader, call read
and assert on the result.
2.how to pass job execution id and job explorer to writer which takes both values as parameters?
Why would an item writer need a job execution and a job explorer? From your code, those are not used when creating the writer.
The step i am supposed to test contains the above reader and writer, i am testing that step with jobLauncherTestUtils.launchStep(stepname,<jobParam>,<executioncontext>). when it goes into writer the jobexplorer is being used to fetch the data from execution context.It works fine when testing as a whole job but not as a single step.
– Hanlak
Dec 29 '18 at 5:34
if your test context is managed by Spring (which I am expecting), then you can inject the job explorer and pass it to the writer.
– Mahmoud Ben Hassine
Dec 29 '18 at 21:52
Actually,I am a newbie to spring and spring batch it would be so helpful for me if you give an example or any link to a sample code.Thanks In Advance
– Hanlak
Dec 30 '18 at 5:56
you can find code examples of how to test a single step or a step-scoped component within a step in this section: docs.spring.io/spring-batch/4.1.x/reference/html/…
– Mahmoud Ben Hassine
Dec 30 '18 at 9:45
I tried what is shown in documentation but i think it still require some add on to add, but i don`t know what is it.could you please help me
– Hanlak
Jan 2 at 17:00
add a comment |
How can i check wheather reader returns the data successfully?
you don't need to test the whole step to check if the reader returns the data correctly. A unit test of the reader itself should be enough: create and open the reader, call read
and assert on the result.
2.how to pass job execution id and job explorer to writer which takes both values as parameters?
Why would an item writer need a job execution and a job explorer? From your code, those are not used when creating the writer.
How can i check wheather reader returns the data successfully?
you don't need to test the whole step to check if the reader returns the data correctly. A unit test of the reader itself should be enough: create and open the reader, call read
and assert on the result.
2.how to pass job execution id and job explorer to writer which takes both values as parameters?
Why would an item writer need a job execution and a job explorer? From your code, those are not used when creating the writer.
answered Dec 28 '18 at 21:48
Mahmoud Ben HassineMahmoud Ben Hassine
5,2651717
5,2651717
The step i am supposed to test contains the above reader and writer, i am testing that step with jobLauncherTestUtils.launchStep(stepname,<jobParam>,<executioncontext>). when it goes into writer the jobexplorer is being used to fetch the data from execution context.It works fine when testing as a whole job but not as a single step.
– Hanlak
Dec 29 '18 at 5:34
if your test context is managed by Spring (which I am expecting), then you can inject the job explorer and pass it to the writer.
– Mahmoud Ben Hassine
Dec 29 '18 at 21:52
Actually,I am a newbie to spring and spring batch it would be so helpful for me if you give an example or any link to a sample code.Thanks In Advance
– Hanlak
Dec 30 '18 at 5:56
you can find code examples of how to test a single step or a step-scoped component within a step in this section: docs.spring.io/spring-batch/4.1.x/reference/html/…
– Mahmoud Ben Hassine
Dec 30 '18 at 9:45
I tried what is shown in documentation but i think it still require some add on to add, but i don`t know what is it.could you please help me
– Hanlak
Jan 2 at 17:00
add a comment |
The step i am supposed to test contains the above reader and writer, i am testing that step with jobLauncherTestUtils.launchStep(stepname,<jobParam>,<executioncontext>). when it goes into writer the jobexplorer is being used to fetch the data from execution context.It works fine when testing as a whole job but not as a single step.
– Hanlak
Dec 29 '18 at 5:34
if your test context is managed by Spring (which I am expecting), then you can inject the job explorer and pass it to the writer.
– Mahmoud Ben Hassine
Dec 29 '18 at 21:52
Actually,I am a newbie to spring and spring batch it would be so helpful for me if you give an example or any link to a sample code.Thanks In Advance
– Hanlak
Dec 30 '18 at 5:56
you can find code examples of how to test a single step or a step-scoped component within a step in this section: docs.spring.io/spring-batch/4.1.x/reference/html/…
– Mahmoud Ben Hassine
Dec 30 '18 at 9:45
I tried what is shown in documentation but i think it still require some add on to add, but i don`t know what is it.could you please help me
– Hanlak
Jan 2 at 17:00
The step i am supposed to test contains the above reader and writer, i am testing that step with jobLauncherTestUtils.launchStep(stepname,<jobParam>,<executioncontext>). when it goes into writer the jobexplorer is being used to fetch the data from execution context.It works fine when testing as a whole job but not as a single step.
– Hanlak
Dec 29 '18 at 5:34
The step i am supposed to test contains the above reader and writer, i am testing that step with jobLauncherTestUtils.launchStep(stepname,<jobParam>,<executioncontext>). when it goes into writer the jobexplorer is being used to fetch the data from execution context.It works fine when testing as a whole job but not as a single step.
– Hanlak
Dec 29 '18 at 5:34
if your test context is managed by Spring (which I am expecting), then you can inject the job explorer and pass it to the writer.
– Mahmoud Ben Hassine
Dec 29 '18 at 21:52
if your test context is managed by Spring (which I am expecting), then you can inject the job explorer and pass it to the writer.
– Mahmoud Ben Hassine
Dec 29 '18 at 21:52
Actually,I am a newbie to spring and spring batch it would be so helpful for me if you give an example or any link to a sample code.Thanks In Advance
– Hanlak
Dec 30 '18 at 5:56
Actually,I am a newbie to spring and spring batch it would be so helpful for me if you give an example or any link to a sample code.Thanks In Advance
– Hanlak
Dec 30 '18 at 5:56
you can find code examples of how to test a single step or a step-scoped component within a step in this section: docs.spring.io/spring-batch/4.1.x/reference/html/…
– Mahmoud Ben Hassine
Dec 30 '18 at 9:45
you can find code examples of how to test a single step or a step-scoped component within a step in this section: docs.spring.io/spring-batch/4.1.x/reference/html/…
– Mahmoud Ben Hassine
Dec 30 '18 at 9:45
I tried what is shown in documentation but i think it still require some add on to add, but i don`t know what is it.could you please help me
– Hanlak
Jan 2 at 17:00
I tried what is shown in documentation but i think it still require some add on to add, but i don`t know what is it.could you please help me
– Hanlak
Jan 2 at 17:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53936364%2fstep-testing-in-spring-batch%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown