Skip to content

Commit

Permalink
Test case严格按方法名排序执行,并在console里打印测试进度。
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Nov 14, 2015
1 parent 13555de commit 4b7a491
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.springside.examples.bootapi.functional;

import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springside.examples.bootapi.BootApiApplication;
import org.springside.modules.test.rule.TestProgress;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BootApiApplication.class)
// 定义为Web集成测试,并使用随机端口号
@WebIntegrationTest("server.port=0")
// 定义每执行完一个Test文件刷新一次Spring Application Context,避免Case间的数据影响.
// 但Test文件内多个测试方法间的影响仍需注意
@DirtiesContext
public abstract class BaseFunctionalTest {

// 注入启动server后的实际端口号
@Value("${local.server.port}")
protected int port;

// 在Console里打印Case的开始与结束,更容易分清Console里的日志归属于哪个Case.
@Rule
public TestRule testProgress = new TestProgress();

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@
import java.util.Map;

import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;
import org.springside.examples.bootapi.BootApiApplication;
import org.springside.examples.bootapi.api.support.ErrorResult;
import org.springside.examples.bootapi.domain.Book;
import org.springside.examples.bootapi.dto.BookDto;
Expand All @@ -42,14 +37,9 @@

import com.google.common.collect.Maps;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BootApiApplication.class)
@WebIntegrationTest("server.port=0")
@DirtiesContext
public class BookEndpointTest {
// 注入启动server后的实际端口号
@Value("${local.server.port}")
private int port;
// 测试方法的执行顺序在不同JVM里是不固定的,此处设为按方法名排序,避免方法间数据影响的不确定性
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class BookEndpointTest extends BaseFunctionalTest {

// 注入Spring Context中的BookDao,实现白盒查询数据库实际情况
@Autowired
Expand Down Expand Up @@ -93,12 +83,16 @@ public void applyRequest() {
String.class, 3L, token);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);

logout(token);

// 查询数据库状态
Book book = bookDao.findOne(3L);
assertThat(book.borrower.id).isEqualTo(1L);
assertThat(book.status).isEqualTo(Book.STATUS_REQUEST);

// 回退操作
response = restTemplate.getForEntity(resourceUrl + "/{id}/cancel?token={token}", String.class, 3L, token);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);

logout(token);
}

@Test
Expand Down Expand Up @@ -150,7 +144,6 @@ public void applyRequestWithError() {
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);

logout(token);

}

@Test
Expand All @@ -173,6 +166,8 @@ public void fullBorrowProcess() {
// 确认归还
response = restTemplate.getForEntity(resourceUrl + "/{id}/return?token={token}", String.class, 1L, token);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);

logout(token);
}

private String login(String user) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.springside.modules.test.rule;

import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

/**
* 在Console里打印Case的开始与结束,更容易分清Console里的日志归属于哪个Case.
*
* @author calvin
*/
public class TestProgress extends TestWatcher {

@Override
protected void starting(Description description) {
System.out.println("\n[Test Case starting] " + description.getTestClass().getSimpleName() + "."
+ description.getMethodName() + "()\n");
}

@Override
protected void finished(Description description) {
System.out.println("\n[Test Case finished] " + description.getTestClass().getSimpleName() + "."
+ description.getMethodName() + "()\n");
}

}

0 comments on commit 4b7a491

Please sign in to comment.