2

LogServiceGAE 以外の単体テストで使用できるように、Google App Engine の「テスト」または「開発」バージョンを使用しようとしています。

クラスパスに と を含めたところ、必要なクラスが表示されappengine-testing.jarましappengine-api.jarた。appengine-api-stubs.jar

com.google.appengine.api.log.dev.LocalLogService

これのインスタンスを作成してfetch(LogQuery)メソッドを検索しようとすると、何も表示されません。明らかに、これは私が探しているクラスではありません。どこが間違っていますか?前もって感謝します。

4

2 に答える 2

2

Mockitoのようなものを使用してGAEをモックしLoggingServiceます。

@Test
public void myUnitTest() {
    // Given
    MyPojo fixture = new MyPojo();

    LoggingService mockService = Mockito.mock(Logging.Service);

    Mockito.doNothing().when(mockService).fetchLogs(Mockito.any());

    fixture.setLoggingService(mockService);

    // When
    fixture.logSomethingToGAELogs("Some string to log");

    // Then - verify
    Mockito.verify(mockService).fetchLogs("Some string to log");
}
于 2013-02-11T19:06:01.890 に答える
0

com.google.appengine.api.log.dev.LocalLogServiceはあなたが探しているものだと思います.com.google.appengine.api.log.LogServiceとは異なるクラス署名を持っている理由はLocalLogServiceは単なる期待です.または中途半端な実装。このクラスは、将来のリリースで変更される可能性があります。

LogService sourceを調べると、fetch メソッドは Iterable<RequestLogs> として形成されたログ レコードを返します。LocalLogService では、ライブ アプリ サーバーからログ レコードをフェッチする代わりに、ログ レコード (通常は Iterable<RequestLogs> のラッパーである LogReadResponse として形成される) をメモリから読み取るだけです。次のサンプル コードは、そのしくみを示しています。

public void foo() {
  String requestId = "1234";

  localLogService = new LocalLogService();

  // Mock a request log record:
  localLogService.addRequestInfo("sample-app", "1", requestId, null, null, startTimeUsec, endTimeUsec, 
          "GET", "", "HTTP/1.1", null, true, 200, null);

  // Mock an app log record:
  localLogService.addAppLogLine(requestId, startTimeUsec, 2, "this is a sample log message.");   

  LogReadRequest request = new LogReadRequest();
  // Filter request 1234:
  request.addRequestId(requestId);
  // Version id must match the one you just mocked, do not use dot in-between:
  request.addVersionId("1");
  request.setIncludeIncomplete(true);
  request.setIncludeAppLogs(true);

  Status status = new Status();
  status.setSuccessful(true);

  // Read mocked log records from memory:
  LogReadResponse response = localLogService.read(status, request);

  for (RequestLog log : response.logs()) {
    System.out.println("request log: " + log.getCombined());
    for (LogLine line : log.lines()) {
      System.out.println("app log: " + line.getLogMessage());
    }
  }
}

出力例:

リクエスト ログ: - - - [8/Feb/2013:09:51:13 -0800] "GET HTTP/1.1" 200 - - -
アプリ ログ: これはサンプル ログ メッセージです。

コード設計を改善するために、LocalLogService を LogService の実装にラップして、クラス シグネチャの一貫性を高めることができます。

于 2013-02-07T21:50:16.940 に答える