用于扩展QuartzJobBean的类的Junits

用于扩展QuartzJobBean的类的Junits,junit,Junit,我有一个Java类,它扩展了QuartzJobBean,并在一天中的特定时间安排 public class ServiceJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) { } 有人能帮我理解如何为此创建Junit测试用例吗。如何在测试用例中调用executeInternal()方法 感谢您在这方面的帮助

我有一个Java类,它扩展了QuartzJobBean,并在一天中的特定时间安排

   public class ServiceJob extends QuartzJobBean {

        @Override
          protected void executeInternal(JobExecutionContext context) {
}
有人能帮我理解如何为此创建Junit测试用例吗。如何在测试用例中调用executeInternal()方法


感谢您在这方面的帮助。

我为我的工作项目创建了一个解决方案,我同意adarshdatt通过导入定义bean的配置文件来解决这个问题。你可以在这里找到一个很好的教程

为了将来的使用,我想展示如何使用Mocking解决它,只需使用Mockito@Mock注释,方法如下:

SessionConfirmationJob.java

public class SessionConfirmationJob extends QuartzJobBean {
    @Autowired
    private SessionService sessionService;
    @Autowired
    private TransactionService transactionService;
    @Autowired
    private SystemLogger systemLogger;
    public static final String TOKEN = "token";
    private SpringInjectQuartzJobBean springInjectQuartzJobBean;

    public SessionService getSessionService() {
        return sessionService;
    }

    public void setSessionService(SessionService sessionService) {
        this.sessionService = sessionService;
    }

    public TransactionService getTransactionService() {
        return transactionService;
    }

    public void setTransactionService(TransactionService transactionService) {
        this.transactionService = transactionService;
    }

    public void setSpringInjectQuartzJobBean(SpringInjectQuartzJobBean springInjectQuartzJobBean) {
        this.springInjectQuartzJobBean = springInjectQuartzJobBean;
    }

    public SystemLogger getSystemLogger() {
        return systemLogger;
    }

    public void setSystemLogger(SystemLogger systemLogger) {
        this.systemLogger = systemLogger;
    }

    @Override
    protected void executeInternal(JobExecutionContext paramJobExecutionContext) throws JobExecutionException {
        springInjectQuartzJobBean = new SpringInjectQuartzJobBean();
        springInjectQuartzJobBean.injectQuartzJobToSpringApplicationContext(this);
        String token = paramJobExecutionContext.getMergedJobDataMap().getString(TOKEN);
        Session session = sessionService.getByToken(token);
        if (session != null) {
            if (session.getPaymentConfirmation() == null || session.getPaymentConfirmation() != true) {
                Transaction transactionToBeRolledBack = transactionService.getRollBackTransactionOfPayment(session);
                if (transactionToBeRolledBack != null) {
                    try {
                        transactionService.rollBackTransaction(transactionToBeRolledBack);
                    } catch (IOException e) {
                        systemLogger.logException("Exception while rolling back transaction", e);
                    }
                    session = sessionService.getByToken(token);
                    session.setStatus(SessionStatus.FI);
                    session.setPaymentConfirmation(false);
                    sessionService.saveOrUpdate(session);
                }
            }
        }
    }
}
这是我应该编写测试的方法,这是测试类

SessionConfirmationJobTest.java

@RunWith(MockitoJUnitRunner.class)
public class SessionConfirmationJobTest {
    @Mock
    private SessionService sessionService;
    @Mock
    private TransactionService transactionService;
    @Mock
    private JobExecutionContext ctx;
    @Mock
    private SpringInjectQuartzJobBean springInjectQuartzJobBean;
    private JobDataMap mergedJobDataMap = new JobDataMap();
    @Mock
    private Scheduler scheduler;
    private SessionConfirmationJob sessionConfirmationJob;
    private String token = "payment token";

    @Before
    public void setUp() throws SchedulerException {
        mergedJobDataMap.put(SessionConfirmationJob.TOKEN, token);
        when(ctx.getMergedJobDataMap()).thenReturn(mergedJobDataMap);
        when(ctx.getScheduler()).thenReturn(scheduler);
        when(scheduler.getContext()).thenReturn(null);
        sessionConfirmationJob = new SessionConfirmationJob();
        sessionConfirmationJob.setSessionService(sessionService);
        sessionConfirmationJob.setTransactionService(transactionService);
        sessionConfirmationJob.setSpringInjectQuartzJobBean(springInjectQuartzJobBean);
    }

    /**
     * Test payment confirmation when we have false payment confirmation
     * 
     * @throws JobExecutionException
     */
    @Test
    public void testPaymentRollBackForFalseConfirmation() throws IOException, JobExecutionException {
        Session session = new Session();
        session.setStatus(SessionStatus.AC);
        session.setPaymentConfirmation(false);
        Transaction transaction = new Transaction();
        transaction.setSession(session);
        transaction.setType(TransactionType.SALE);
        transaction.setStatus(TransactionStatus.AP);
        when(sessionService.getByToken(token)).thenReturn(session);
        when(transactionService.getRollBackTransactionOfPayment(session)).thenReturn(transaction);
        when(transactionService.rollBackTransaction(transaction)).thenReturn(true);
        sessionConfirmationJob.execute(ctx);
        Assert.assertEquals(SessionStatus.FI, session.getStatus());
        Assert.assertFalse(session.getPaymentConfirmation());
        verify(sessionService).saveOrUpdate(session);
    }
}
public abstract class QuartzJobBean implements Job {

    /**
     * This implementation applies the passed-in job data map as bean property
     * values, and delegates to {@code executeInternal} afterwards.
     * @see #executeInternal
     */
    @Override
    public final void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.addPropertyValues(context.getScheduler().getContext());
            pvs.addPropertyValues(context.getMergedJobDataMap());
            bw.setPropertyValues(pvs, true);
        }
        catch (SchedulerException ex) {
            throw new JobExecutionException(ex);
        }
        executeInternal(context);
    }

/**
 * Execute the actual job. The job data map will already have been
 * applied as bean property values by execute. The contract is
 * exactly the same as for the standard Quartz execute method.
 * @see #execute
 */
protected abstract void executeInternal(JobExecutionContext context) throws JobExecutionException;
在模拟调度对象之前,我在
pvs.addPropertyValues(context.getScheduler().getContext())处获得NullPointerException在我模拟调度后,它被解决了,我的测试通过了。以下是

org.springframework.scheduling.quartz.QuartzJobBean#execute(JobExecutionContext)
方法。实际上,executeInternal是受保护的,所以我们必须首先调用execute方法,然后execute方法是call executeInternal,它在实现的作业类中被重写(我的演示是SessionConfigurationJob)

QuartzJobBean.java

@RunWith(MockitoJUnitRunner.class)
public class SessionConfirmationJobTest {
    @Mock
    private SessionService sessionService;
    @Mock
    private TransactionService transactionService;
    @Mock
    private JobExecutionContext ctx;
    @Mock
    private SpringInjectQuartzJobBean springInjectQuartzJobBean;
    private JobDataMap mergedJobDataMap = new JobDataMap();
    @Mock
    private Scheduler scheduler;
    private SessionConfirmationJob sessionConfirmationJob;
    private String token = "payment token";

    @Before
    public void setUp() throws SchedulerException {
        mergedJobDataMap.put(SessionConfirmationJob.TOKEN, token);
        when(ctx.getMergedJobDataMap()).thenReturn(mergedJobDataMap);
        when(ctx.getScheduler()).thenReturn(scheduler);
        when(scheduler.getContext()).thenReturn(null);
        sessionConfirmationJob = new SessionConfirmationJob();
        sessionConfirmationJob.setSessionService(sessionService);
        sessionConfirmationJob.setTransactionService(transactionService);
        sessionConfirmationJob.setSpringInjectQuartzJobBean(springInjectQuartzJobBean);
    }

    /**
     * Test payment confirmation when we have false payment confirmation
     * 
     * @throws JobExecutionException
     */
    @Test
    public void testPaymentRollBackForFalseConfirmation() throws IOException, JobExecutionException {
        Session session = new Session();
        session.setStatus(SessionStatus.AC);
        session.setPaymentConfirmation(false);
        Transaction transaction = new Transaction();
        transaction.setSession(session);
        transaction.setType(TransactionType.SALE);
        transaction.setStatus(TransactionStatus.AP);
        when(sessionService.getByToken(token)).thenReturn(session);
        when(transactionService.getRollBackTransactionOfPayment(session)).thenReturn(transaction);
        when(transactionService.rollBackTransaction(transaction)).thenReturn(true);
        sessionConfirmationJob.execute(ctx);
        Assert.assertEquals(SessionStatus.FI, session.getStatus());
        Assert.assertFalse(session.getPaymentConfirmation());
        verify(sessionService).saveOrUpdate(session);
    }
}
public abstract class QuartzJobBean implements Job {

    /**
     * This implementation applies the passed-in job data map as bean property
     * values, and delegates to {@code executeInternal} afterwards.
     * @see #executeInternal
     */
    @Override
    public final void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.addPropertyValues(context.getScheduler().getContext());
            pvs.addPropertyValues(context.getMergedJobDataMap());
            bw.setPropertyValues(pvs, true);
        }
        catch (SchedulerException ex) {
            throw new JobExecutionException(ex);
        }
        executeInternal(context);
    }

/**
 * Execute the actual job. The job data map will already have been
 * applied as bean property values by execute. The contract is
 * exactly the same as for the standard Quartz execute method.
 * @see #execute
 */
protected abstract void executeInternal(JobExecutionContext context) throws JobExecutionException;
}


如果您有问题,请随时通过评论询问我。

问题通过导入定义beanerhun的配置文件得到解决。我在数据库中有Cron表达式,我正在获取它们。首先,我如何能够c检查Cron表达式,然后通过Cron表达式触发。