私は 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 という結果になりました。
この背後にある理由が何であるかを明らかにしてください。もう1つの観察は、昨日はうまくいき、突然この問題が明らかになったということです.
誰かがこの問題を解決するのを手伝ってくれれば、とても助かります。