alejo...@nomasystems.com
unread,Apr 19, 2013, 11:35:05 AM4/19/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to rest-a...@googlegroups.com
Hi!
I'm using a PrintStream different from System.out to print the log to something handable by log4j.
Example:
Suite
private static StringWriter writer = new StringWriter();
private static WriterOutputStream writerOutputStream =
new WriterOutputStream(writer, Charset.defaultCharset(), 1024, true);
private static PrintStream defaultPrintStream = new PrintStream(writerOutputStream, true);
private static Logger primaryLogger; // To screen.
private static Logger secondaryLogger; // To file.
@BeforeClass
public static void init() {
RestAssured.config = config().logConfig(logConfig().defaultStream(defaultPrintStream));
...
}
public static String getRestAssuredLog() {
writer.flush();
String restAssuredLog = writer.toString();
StringBuffer buffer = writer.getBuffer();
buffer.replace(0, buffer.length(), "");
return restAssuredLog;
}
Facade
// Only a pair of examples.
private RequestSpecification requestSpecification(RequestData requestData) {
RequestSpecification requestSpecification = given();
Iterator<String> names = requestData.pathParamsIterator();
while (names.hasNext()) {
String name = names.next();
requestSpecification = requestSpecification.pathParam(name, requestData.pathParamsGet(name));
}
names = requestData.queryParamsIterator();
while (names.hasNext()) {
String name = names.next();
requestSpecification = requestSpecification.queryParam(name, requestData.queryParamsGet(name));
}
if (requestData.getHeaders() != null) {
requestSpecification = requestSpecification.headers(requestData.getHeaders());
}
if (requestData.getBody() != null) {
requestSpecification = requestSpecification.contentType(ContentType.JSON).body(requestData.getBody().toString());
}
if (debug) {
if (log) {
requestSpecification = requestSpecification.log().all();
String restAssuredLog = getRestAssuredLog();
if (!restAssuredLog.equalsIgnoreCase("")) {
getSecondaryLogger().info(restAssuredLog);
}
}
}
return requestSpecification;
}
...
private ResponseSpecification responseSpecification(RequestSpecification requestSpecification, int statusCode) {
ResponseSpecification responseSpecification = requestSpecification.expect().statusCode(statusCode);
if (log) {
if (debug) {
responseSpecification = responseSpecification.log().all();
} else {
responseSpecification = responseSpecification.log().ifError();
}
String restAssuredLog = getRestAssuredLog();
if (!restAssuredLog.equalsIgnoreCase("")) {
getSecondaryLogger().info(restAssuredLog);
}
}
return responseSpecification;
}
...
It works fine printing to System.out, but when I configure it to print to other PrintStream (which finally prints to the StringWriter), it prints all right except the last request/response. So, when a test fails and I need to see the output in the log file, I can't, because it is the last one and it is not printed.
After a lot of tests trying to discard several things, I'm sure it is not a log4j problem. And I have no more ideas to find it. So, can anyone help me? I'm not sure at all, but could even be a rest-assured bug?
Thanks in advance,
Alejo.