Java 使用JUnit中的Blobstore

Java 使用JUnit中的Blobstore,java,google-app-engine,Java,Google App Engine,我正在尝试测试一些使用Blobstore API的代码,但我不知道如何将一些文件放入Blobstore。以下操作不起作用: private BlobKey createBlob(String path) throws Exception { FileService fileService = FileServiceFactory.getFileService(); AppEngineFile file = fileService.createNewBlobFile("foobar

我正在尝试测试一些使用Blobstore API的代码,但我不知道如何将一些文件放入Blobstore。以下操作不起作用:

private BlobKey createBlob(String path) throws Exception {
    FileService fileService = FileServiceFactory.getFileService();
    AppEngineFile file = fileService.createNewBlobFile("foobar");
    FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
    OutputStream output = Channels.newOutputStream(writeChannel);

    // copy files, guava-style
    InputStream input = new FileInputStream(path);
    assertNotNull(input);
    ByteStreams.copy(input, output); 
    input.close();

    // just in case...
    output.flush();
    output.close();
    writeChannel.close();

    // U NO WORK!!!
    BlobKey blobKey = fileService.getBlobKey(file);
    assertNotNull(blobKey);
    return blobKey;
}
我的配置:

new LocalServiceTestHelper(
    new LocalBlobstoreServiceTestConfig()
        //.setNoStorage(true)
        .setBackingStoreLocation("war/WEB-INF/appengine-generated"),
    new LocalFileServiceTestConfig()
).setUp();

有什么想法吗?

以下测试运行成功

public class TestBlobstore {
  private static final LocalServiceTestHelper helper = 
    new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig(),
                               new LocalBlobstoreServiceTestConfig()
                              );

  public TestBlobstore() {
  }

  @Before
  public void setUp() {
    helper.setUp();
  }

  @Test
  public void testBlobstore() throws Exception {
    System.out.println(createBlob("test.txt"));
  }

  private BlobKey createBlob(String path) throws Exception {
    FileService fileService = FileServiceFactory.getFileService();
    AppEngineFile file = fileService.createNewBlobFile("foobar");
    FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
    OutputStream output = Channels.newOutputStream(writeChannel);

    // copy files, guava-style
    InputStream input = new FileInputStream(path);
    assertNotNull(input);
    ByteStreams.copy(input, output); 
    input.close();

    // just in case...
    output.flush();
    output.close();
    writeChannel.closeFinally();

    // U NO WORK!!!
    BlobKey blobKey = fileService.getBlobKey(file);
    assertNotNull(blobKey);
    return blobKey;
  }
}
两项修改:

  • 使用LocalBlobstoreServiceTestConfig而不是LocalFileServiceTestConfig
  • writeChannel.closeFinally();而不是writeChannel.close()

希望有帮助。

您正在测试BlobStore功能吗?这将是奇怪的,因为它自己的开发者应该对此进行测试。听起来像是要模拟BlobStore对象,并针对thisNope进行断言和测试。我只是想测试我的代码,但我需要Blobstore中的一些文件来完成。这只是我测试设置的一部分。你的代码在什么意义上不起作用?更好的是,发布一个其他人可以运行的简单复制案例。例如,最好查看LocalServiceTestHelper的实际实例化位置。此测试返回“com.google.apphosting.api.ApiProxy$CallNotFoundException:找不到api包'file'或调用'Create()'。你知道我错了什么或遗漏了什么吗?别担心,使用“API包未找到”下的说明就成功了。这在AppEngine 1.9.28中不再适用。有人有别的办法吗?