Spring 弹簧批测试用例
我对Spring批处理和测试用例非常陌生,我创建了一个示例程序,它从表中获取数据,将名称更新为大写,并保存回数据库 处理机Spring 弹簧批测试用例,spring,spring-boot,spring-batch,spring-test,Spring,Spring Boot,Spring Batch,Spring Test,我对Spring批处理和测试用例非常陌生,我创建了一个示例程序,它从表中获取数据,将名称更新为大写,并保存回数据库 处理机 import com.example.demo.demoWeb.model.User; import org.springframework.batch.item.ItemProcessor; import org.springframework.stereotype.Component; @Component public class Processor implemen
import com.example.demo.demoWeb.model.User;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;
@Component
public class Processor implements ItemProcessor<User, User> {
@Override
public User process(User user) throws Exception {
System.out.println("Processor.process user = " + user);
user.setName(user.getName().toUpperCase());
return user;
}
}
存储库
import com.example.demo.demoWeb.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Integer> {
}
梯度锉
plugins {
id 'org.springframework.boot' version '2.3.0.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.example.demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-batch'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.batch:spring-batch-test'
implementation 'mysql:mysql-connector-java'
compile 'org.apache.tomcat:tomcat-dbcp:9.0.1'
}
test {
useJUnitPlatform()
}
我已经浏览了4-5页的google,但仍然找不到任何带有测试用例的SpringBatch的工作示例。
大多数例子都太抽象了,我无法与它们联系起来
到目前为止,我想出了这个。但它失败了,我不知道这有什么问题
import com.example.demo.demoWeb.config.SpringBatchConfig;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.JobRepositoryTestUtils;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import static org.junit.Assert.assertEquals;
@RunWith(SpringRunner.class)
@SpringBatchTest
@EnableAutoConfiguration
@ContextConfiguration(classes = {SpringBatchConfig.class})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class DemoWebApplicationTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private JobRepositoryTestUtils jobRepositoryTestUtils;
@After
public void cleanUp() {
jobRepositoryTestUtils.removeJobExecutions();
}
@Test
public void launchJob() throws Exception {
//testing a job
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
//Testing a individual step
//JobExecution jobExecution = jobLauncherTestUtils.launchStep("step1");
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
}
}
我知道JUnit测试用例,但对spring批处理一无所知。请指导我如何编写spring批处理测试用例。很难说到底出了什么问题。但是,您正在应用程序中使用Spring Boot。用@SpringBootTest替换注释@ContextConfiguration可以使您受益匪浅 SpringBoot提供了@SpringBootTest注释,可以用作 标准spring测试@ContextConfiguration的替代方案 需要Spring启动功能时的注释 试试这个:
@SpringBatchTest
@SpringBootTest
@RunWith(SpringRunner.class)
public class DemoWebApplicationTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private JobRepositoryTestUtils jobRepositoryTestUtils;
@After
public void cleanUp() {
jobRepositoryTestUtils.removeJobExecutions();
}
@Test
public void launchJob() throws Exception {
//testing a job
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
//Testing a individual step
//JobExecution jobExecution = jobLauncherTestUtils.launchStep("step1");
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
}
}
谢谢leiblix,它成功了,你能告诉我这个示例程序还可以运行哪些测试用例吗。它将让我了解批处理应用程序中的测试用例应该是什么以及如何。这实际上调用了批处理作业吗?我们如何使其成为基于模拟的?
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
@EnableScheduling
@SpringBootApplication
public class DemoWebApplication {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
public static void main(String[] args) {
SpringApplication.run(DemoWebApplication.class, args);
}
@Scheduled(cron = "0 */1 * * * ?")
public void perform() throws Exception {
JobParameters params = new JobParametersBuilder()
.addString("JobID", String.valueOf(System.currentTimeMillis()))
.toJobParameters();
jobLauncher.run(job, params);
}
}
plugins {
id 'org.springframework.boot' version '2.3.0.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.example.demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-batch'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.batch:spring-batch-test'
implementation 'mysql:mysql-connector-java'
compile 'org.apache.tomcat:tomcat-dbcp:9.0.1'
}
test {
useJUnitPlatform()
}
import com.example.demo.demoWeb.config.SpringBatchConfig;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.JobRepositoryTestUtils;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import static org.junit.Assert.assertEquals;
@RunWith(SpringRunner.class)
@SpringBatchTest
@EnableAutoConfiguration
@ContextConfiguration(classes = {SpringBatchConfig.class})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class DemoWebApplicationTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private JobRepositoryTestUtils jobRepositoryTestUtils;
@After
public void cleanUp() {
jobRepositoryTestUtils.removeJobExecutions();
}
@Test
public void launchJob() throws Exception {
//testing a job
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
//Testing a individual step
//JobExecution jobExecution = jobLauncherTestUtils.launchStep("step1");
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
}
}
@SpringBatchTest
@SpringBootTest
@RunWith(SpringRunner.class)
public class DemoWebApplicationTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private JobRepositoryTestUtils jobRepositoryTestUtils;
@After
public void cleanUp() {
jobRepositoryTestUtils.removeJobExecutions();
}
@Test
public void launchJob() throws Exception {
//testing a job
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
//Testing a individual step
//JobExecution jobExecution = jobLauncherTestUtils.launchStep("step1");
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
}
}