我正在开发基于 Play 2.0 的 RESTful API 实现,当我尝试运行测试用例(CRUD 操作)时,我看到 POST 请求内容(成功的 201 响应)在随后执行 GET 操作时不可用测试用例。
请看一下我的 JUnit 测试类 -
public class TagTest {
public static FakeApplication app;
private static String AUTH_HEADER = "Authorization";
private static String AUTH_VALUE = "Basic a25paadsfdfasdfdsfasdmeSQxMjM=";
private static int tagId = 0;
private static Map<String, String> postDataMap = new HashMap<String, String>();
private static Map<String, String> updateDataMap = new HashMap<String, String>();
private static String searchText = null;
@BeforeClass
public static void setUpBeforeClass() {
// Set up new FakeApplication before running any tests
app = Helpers.fakeApplication();
Helpers.start(app);
postDataMap.put("text", "Created");
updateDataMap.put("text", "Updated");
searchText = "Date"; // case insensitive substring pattern for "Updated"
}
@Test
public void createTagTest() {
app = Helpers.fakeApplication();
running(fakeApplication(), new Runnable() {
public void run() {
JsonNode json = Json.toJson(postDataMap);
FakeRequest request=new FakeRequest().withJsonBody(json);
Result result = callAction(controllers.routes.ref.Application.createTag(),request.withHeader(TagTest.AUTH_HEADER, TagTest.AUTH_VALUE));
Map<String, String> headerMap = Helpers.headers(result);
String location = headerMap.get(Helpers.LOCATION);
String tagIdStr = location.replace("/tags/","");
try {
tagId = Integer.parseInt(tagIdStr);
assertThat(status(result)).isEqualTo(Helpers.CREATED);
System.out.println("Tag Id : "+tagId+" Location : "+headerMap.get(Helpers.LOCATION)); // Here I'm getting resource URI from API which means it is successful run
} catch (NumberFormatException e) {
System.out.println("Inside NumberFormatException");
e.printStackTrace();
assertThat(0).isEqualTo(1);
}
System.out.println("createTagTest is successful");
}
});
}
@Test
public void getTagTest() {
app = Helpers.fakeApplication();
running(fakeApplication(), new Runnable() {
public void run() {
FakeRequest request = new FakeRequest();
Result result = callAction(controllers.routes.ref.Application.getTag(tagId), request.withHeader(TagTest.AUTH_HEADER, TagTest.AUTH_VALUE));
String content = contentAsString(result);
if(content.length()==0) {
assertThat(status(result)).isEqualTo(Helpers.NO_CONTENT);
} else {
assertThat(status(result)).isEqualTo(Helpers.OK);
}
System.out.println("getTagTest is successful");
}
});
}
@Test
public void updateTagTest() {
app = Helpers.fakeApplication();
running(fakeApplication(), new Runnable(){
public void run() {
JsonNode json = Json.toJson(updateDataMap);
FakeRequest request = new FakeRequest().withJsonBody(json);
Result result = callAction(controllers.routes.ref.Application.updateTag(tagId),
request.withHeader(TagTest.AUTH_HEADER, TagTest.AUTH_VALUE));
assertThat(status(result)).isEqualTo(Helpers.OK);
System.out.println("updateTagTest is successful");
}
});
}
@Test
public void getAllTagsTest() {
app = Helpers.fakeApplication();
running(fakeApplication(), new Runnable() {
public void run() {
FakeRequest request = new FakeRequest();
Result result = callAction(controllers.routes.ref.Application.getAllTags(null), request.withHeader(TagTest.AUTH_HEADER, TagTest.AUTH_VALUE));
String content = contentAsString(result);
System.out.println(content);
if(content.length()==0) {
System.out.println("No content");
assertThat(status(result)).isEqualTo(Helpers.NO_CONTENT);
} else {
System.out.println("Content");
assertThat(status(result)).isEqualTo(Helpers.OK);
}
System.out.println("getAllTagsTest is successful");
}
});
}
@Test
public void getTagsByTextTest() {
app = Helpers.fakeApplication();
running(fakeApplication(), new Runnable() {
public void run() {
FakeRequest request = new FakeRequest();
Result result = callAction(controllers.routes.ref.Application.getAllTags(searchText), request.withHeader(TagTest.AUTH_HEADER, TagTest.AUTH_VALUE));
String content = contentAsString(result);
if(content.length()==0) {
assertThat(status(result)).isEqualTo(Helpers.NO_CONTENT);
} else {
assertThat(status(result)).isEqualTo(Helpers.OK);
}
System.out.println("getAllTagsByTextTest is successful");
}
});
}
@Test
public void deleteTagTest() {
app = Helpers.fakeApplication();
running(fakeApplication(), new Runnable() {
public void run() {
FakeRequest request = new FakeRequest();
Result result = callAction(controllers.routes.ref.Application.deleteTag(tagId), request.withHeader(TagTest.AUTH_HEADER, TagTest.AUTH_VALUE));
assertThat(status(result)).isEqualTo(Helpers.OK);
System.out.println("deleteTagTest is successful");
}
});
}
@AfterClass
public static void tearDownAfterClass() {
// Stop FakeApplication after all tests complete
Helpers.stop(app);
}
}
当我运行测试时,创建了标签,但在尝试执行 GET /tags/1 并导致 204 No content 时未在后续测试中拾取它。
请阐明这背后的原因。另一个观察结果是,它昨天奏效了,突然间这个问题就出现了。
如果有人可以帮助我解决此问题,将有很大帮助。