我正在尝试使用jUnit在春季批处理中测试一项工作,但我不知道如何解决该错误。
我有这堂课
ParametersValidatorService
@Service("parametersValidator")
public class ParametersValidatorService implements IParameterValidatorService {
/**
* The Constant LOGGER.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(ParametersValidatorService.class);
/**
* Array con los parametros obligatorios.
*/
private static final String[] PARAMETROS_OBLIGATORIOS = new String[]{
"val1", "val2", "val3", "val4", "val5", "val6", "val7"
};
/**
* Array con los parametros que deben ser numericos.
*/
private static final String[] PARAMETROS_NUMERICOS = new String[]{
"val1", "val2", "val3", "val4"};
/**
* @param jobId: the job id
* @param parameters: input job parameters
* ParametersValidatorException: manages exceptions related with input parameters.
*/
@Override
public void validate(final String jobId, final JobParameters parameters) throws ParametersValidatorException {
try {
validate(parameters);
} catch (JobParametersInvalidException e) {
throw new ParametersValidatorException(e);
}
}
/**
* @param parameters: input job parameters
* JobParametersInvalidException: manages exceptions related with input parameters.
*/
@Override
public void validate(final JobParameters parameters) throws JobParametersInvalidException {
validarParametros(parameters);
}
/**
* Comprueba que los parámetros no estén vacíos y que los que tienen que ser numéricos, sean numéricos
*
* @param parameters: input job parameters
* JobParametersInvalidException: manages exceptions related with input parameters.
*/
private void validarParametros(final JobParameters parameters) throws JobParametersInvalidException {
List<String> numericParameters = Arrays.asList(PARAMETROS_NUMERICOS);
String error = null;
for (String validacion : PARAMETROS_OBLIGATORIOS) {
if (StringUtils.isBlank(parameters.getString(validacion))) {
error = "El parametro " + validacion + " es obligatorio";
} else if (numericParameters.contains(validacion) && !StringUtils.isNumeric(parameters.getString(validacion))) {
error = "El parametro " + validacion + " debe ser numerico";
}
if (error != null) {
LOGGER.error(error);
throw new JobParametersInvalidException(error);
}
}
}
}
我正在尝试使用jUnit4进行测试,但是我无法正确执行它。我做了这个测试
ParametersValidatorServiceTest
@RunWith(SpringJUnit4ClassRunner.class)
public class ParametersValidatorServiceTest {
//@Autowired
@Bean
public JobLauncherTestUtils jobLauncherTestUtils() {
return new JobLauncherTestUtils();
}
@Test
public void testLaunchJobOK() throws Exception {
JobParameters jobParameters = new JobParametersBuilder().addString("val1", "1")
.addString("val2", "Nombre de prueba")
.addString("val3", "61").addString("val6", "1")
.addString("val4", "RCVD").addString("val7", "43")
.addString("val5", "2017-05-10 10:48:42").toJobParameter
JobExecution jobExecution = jobLauncherTestUtils().launchJob(jobParameters);
Assert.assertEquals(ExitStatus.COMPLETED.getExitCode(), jobExecution.getExitStatus().getExitCode());
}
}
我总是收到如下所示的java.lang.NullPointerException
java.lang.NullPointerException
at org.springframework.batch.test.JobLauncherTestUtils.launchJob(JobLauncherTestUtils.java:152)
at com.bbva.kyob.respuestasob.services.impl.ParametersValidatorServiceTest.testLaunchJobOK(ParametersValidatorServiceTest.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
调试测试时,我发现输入的参数已被收集并存储在jobParameters中,所以我不知道发生了什么
答案 0 :(得分:1)
如果考虑一下,在ParametersValidatorServiceTest
类中没有引用任何作业,对吗?因此,当您执行以下语句时:
JobExecution jobExecution = jobLauncherTestUtils().launchJob(jobParameters);
应该启动哪个职位?该工作实际上是JobLauncherTestUtils
中的a dependency。这就是为什么应该在测试上下文中定义它以便在JobLauncherTestUtils
中正确自动接线的原因。例如,您可以导入包含其定义的配置类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {MyJobConfiguration.class}) // contains the job bean under test
public class ParametersValidatorServiceTest {
// ...
}
希望这会有所帮助。