Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Created April 8, 2012 02:11
Show Gist options
  • Save mike-neck/2333740 to your computer and use it in GitHub Desktop.
Save mike-neck/2333740 to your computer and use it in GitHub Desktop.
@ruleアノテーションを利用したAppengineTestCase
package org.mikeneck.gae.slim3.sample.util;
import org.junit.rules.ExternalResource;
import org.slim3.tester.AppEngineTester;
/**
* @author : mike
* @since : 12/04/08
*/
public class AppengineExternalResource extends ExternalResource {
private AppEngineTester tester = new AppEngineTester();
@Override
protected void after() throws RuntimeException{
try {
tester.tearDown();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Override
protected void before() throws Throwable {
tester.setUp();
}
}
package org.junit.rules;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
* A base class for Rules (like TemporaryFolder) that set up an external
* resource before a test (a file, socket, server, database connection, etc.),
* and guarantee to tear it down afterward:
*
* <pre>
* public static class UsesExternalResource {
* Server myServer= new Server();
*
* &#064;Rule
* public ExternalResource resource= new ExternalResource() {
* &#064;Override
* protected void before() throws Throwable {
* myServer.connect();
* };
*
* &#064;Override
* protected void after() {
* myServer.disconnect();
* };
* };
*
* &#064;Test
* public void testFoo() {
* new Client().run(myServer);
* }
* }
* </pre>
*/
public abstract class ExternalResource implements TestRule {
public Statement apply(Statement base, Description description) {
return statement(base);
}
private Statement statement(final Statement base) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
before();
try {
base.evaluate();
} finally {
after();
}
}
};
}
/**
* Override to set up your specific external resource.
* @throws if setup fails (which will disable {@code after}
*/
protected void before() throws Throwable {
// do nothing
}
/**
* Override to tear down your specific external resource.
*/
protected void after() {
// do nothing
}
}
package org.mikeneck.gae.slim3.sample;
import com.google.appengine.api.datastore.Key;
import org.slim3.datastore.Attribute;
import org.slim3.datastore.Model;
import java.util.Date;
/**
* @author : mike
* @since : 12/04/08
*/
@Model (schemaVersion = 1)
public class Message {
@Attribute(primaryKey = true)
Key key;
@Attribute
String message;
@Attribute
Date createdAt;
@Attribute
Date expiredAt;
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getExpiredAt() {
return expiredAt;
}
public void setExpiredAt(Date expiredAt) {
this.expiredAt = expiredAt;
}
public Key getKey() {
return key;
}
public void setKey(Key key) {
this.key = key;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package org.mikeneck.gae.slim3.sample;
import com.google.appengine.api.datastore.Key;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExternalResource;
import org.mikeneck.gae.slim3.sample.util.AppengineExternalResource;
import org.slim3.datastore.Datastore;
import java.util.Date;
import java.util.List;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* @author : mike
* @since : 12/04/08
*/
public class MessageTest {
@Rule
public static ExternalResource resource = new AppengineExternalResource();
private static final MessageMeta meta = MessageMeta.get();
@Test
public void countData () {
Message message = new Message();
message.setCreatedAt(new Date());
message.setMessage("test");
Key key = Datastore.put(message);
Message stored = Datastore.get(meta, key);
assertThat(stored.getMessage(), is("test"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment