Google appengine Java を使用しています。
Junit テストで、例外 DatastoreTimeoutException と DatastoreFailureException を発生させたいと考えています。スロー例外を発生させるにはどうすればよいですか?
Google appengine Java を使用しています。
Junit テストで、例外 DatastoreTimeoutException と DatastoreFailureException を発生させたいと考えています。スロー例外を発生させるにはどうすればよいですか?
はっきりしませんでした。
Google Appengineサーバーは、データストアが応答しない場合にDatastoreTimeoutExceptionを生成し、データストアが不明な例外をスローした場合にDatastoreFailureExceptionを生成する可能性があります。
私のJunitテストでは、サーバーがこれらの例外をスローするようにしたいと思います。サーバーの機能を使用するようにhttps://developers.google.com/appengine/docs/java/capabilities/overview
これらの例外をキャッチすることを確認するために、このように書きたいと思います。
@Test
public final void testDatastoreFailureException() {
// config the datastore in order to throw the exception
try {
// do something using the datastore.
} catch ( DatastoreFailureException e ) {
fail("We do not catch the exception DatastoreFailureException");
}
}
JUnit テストでは、作成が許可されている任意の例外をスローできます。その例外をスローするようにテストをマークしてください (または、多くの場合、例外をスローできることを示します)。
例えば
@Test
public void testSomething() throws Exception {
throw new WhateverYouFancy("Hello, world!");
}
または、特定の例外がスローされたことを確認したい場合 (つまり、これがテストの成功を示しているため) は、次のようにします。
@Test(expected=FancyException.class)
public void testSomething() throws Exception {
// do something that ought to trigger a FancyException
}