Skip to content

Commit

Permalink
加强用例健壮性
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Oct 6, 2012
1 parent 9c738c0 commit d914e27
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<prop key="org.quartz.threadPool.threadCount">5</prop>
</props>
</property>
<!-- 启动时延期3秒开始任务 -->
<property name="startupDelay" value="3" />
<!-- 启动时延期2秒开始任务 -->
<property name="startupDelay" value="2" />
</bean>

<!-- Cron式Trigger定义 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.junit.Assert.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.test.annotation.DirtiesContext;
Expand All @@ -16,20 +18,27 @@
@ContextConfiguration(locations = { "/applicationContext.xml", "/schedule/applicationContext-jdk-timer.xml" })
public class JdkTimerJobTest extends SpringTransactionalTestCase {

@Test
public void scheduleJob() throws Exception {
private static Log4jMockAppender appender;

//加载测试用logger appender
Log4jMockAppender appender = new Log4jMockAppender();
@BeforeClass
public static void initLogger() {
// 加载测试用logger appender
appender = new Log4jMockAppender();
appender.addToLogger(UserCountScanner.class);
}

@AfterClass
public static void removeLogger() {
appender.removeFromLogger(UserCountScanner.class);
}

//等待任务启动
@Test
public void scheduleJob() throws Exception {
// 等待任务延时1秒启动并执行完毕
Threads.sleep(3000);

//验证任务已执行
// 验证任务已执行
assertEquals(1, appender.getLogsCount());
assertEquals("There are 6 user in database, printed by jdk timer job.", appender.getFirstMessage());

appender.removeFromLogger(UserCountScanner.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.junit.Assert.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.test.annotation.DirtiesContext;
Expand All @@ -21,22 +23,29 @@
@ContextConfiguration(locations = { "/applicationContext.xml", "/schedule/applicationContext-quartz-timer-cluster.xml" })
public class QuartzTimerClusterJobTest extends SpringTransactionalTestCase {

@Test
public void scheduleJob() throws Exception {
private static Log4jMockAppender appender;

//加载测试用logger appender
Log4jMockAppender appender = new Log4jMockAppender();
@BeforeClass
public static void initLogger() {
// 加载测试用logger appender
appender = new Log4jMockAppender();
appender.addToLogger(QuartzClusterableJob.class);
}

@AfterClass
public static void removeLogger() {
appender.removeFromLogger(QuartzClusterableJob.class);
}

//等待任务延时启动
@Test
public void scheduleJob() throws Exception {
// 等待任务延时2秒启动并执行完毕
Threads.sleep(4000);

//验证任务已执行
// 验证任务已执行
assertEquals(1, appender.getLogsCount());

assertEquals("There are 6 user in database, printed by quartz cluster job on node default.",
appender.getFirstMessage());

appender.removeFromLogger(QuartzClusterableJob.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.junit.Assert.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.test.annotation.DirtiesContext;
Expand All @@ -16,19 +18,27 @@
@ContextConfiguration(locations = { "/applicationContext.xml", "/schedule/applicationContext-spring-scheduler.xml" })
public class SpringTimerJobTest extends SpringTransactionalTestCase {

@Test
public void scheduleJob() throws Exception {
private static Log4jMockAppender appender;

@BeforeClass
public static void initLogger() {
// 加载测试用logger appender
Log4jMockAppender appender = new Log4jMockAppender();
appender = new Log4jMockAppender();
appender.addToLogger(UserCountScanner.class);
}

// 等待任务启动
Threads.sleep(3000);
@AfterClass
public static void removeLogger() {
appender.removeFromLogger(UserCountScanner.class);
}

@Test
public void scheduleJob() throws Exception {
// 等待任务执行完毕
Threads.sleep(2000);

// 验证任务已执行
assertEquals(1, appender.getLogsCount());
assertEquals("There are 6 user in database, printed by spring timer job by xml.", appender.getFirstMessage());
appender.removeFromLogger(UserCountScanner.class);
}
}

0 comments on commit d914e27

Please sign in to comment.