Skip to content

Instantly share code, notes, and snippets.

@sue445
Created September 7, 2012 15:43
Show Gist options
  • Save sue445/3667305 to your computer and use it in GitHub Desktop.
Save sue445/3667305 to your computer and use it in GitHub Desktop.
Theoriesのサンプル
package net.sue445.azusaar.util;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import javax.servlet.http.Cookie;
import net.sue445.azusaar.util.SessionUtil.SessionKey;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import org.slim3.tester.AppEngineTestCase;
import org.slim3.tester.ControllerTestCase;
import twitter4j.Twitter;
@RunWith(Enclosed.class)
public class TwitterUtilTest{
public static class GetTwitter extends AppEngineTestCase {
@Test
public void getTwitter() throws Exception {
Twitter actual = TwitterUtil.getTwitter(TestUtil.ACCESS_TOKEN, TestUtil.ACCESS_TOKEN_SECRET);
assertThat(actual, is(notNullValue()));
assertThat(actual.verifyCredentials().getScreenName(), is("sue445"));
}
}
@RunWith(Theories.class)
public static class IsAccessTokenValid extends AppEngineTestCase{
@DataPoints
public static Fixture[] FIXTURES = {
new Fixture(TestUtil.ACCESS_TOKEN, TestUtil.ACCESS_TOKEN_SECRET, true),
new Fixture("", "", false),
new Fixture("aaa", "bbb", false),
};
static class Fixture{
String accessToken;
String accessTokenSecret;
boolean expected;
public Fixture(String accessToken, String accessTokenSecret, boolean expected) {
this.accessToken = accessToken;
this.accessTokenSecret = accessTokenSecret;
this.expected = expected;
}
}
@Theory
public void isAccessTokenValid(Fixture fixture) throws Exception {
boolean actual = TwitterUtil.isAccessTokenValid(fixture.accessToken, fixture.accessTokenSecret);
String message = "accessToken=" + fixture.accessToken + ", accessTokenSecret=" + fixture.accessTokenSecret;
assertThat(message, actual, is(fixture.expected));
}
}
public static class WhenHasCookie extends ControllerTestCase {
@Before
public void before() {
tester.request.addCookie(new Cookie(CookieUtil.ACCESS_TOKEN, TestUtil.ACCESS_TOKEN));
tester.request.addCookie(new Cookie(CookieUtil.ACCESS_TOKEN_SECRET, TestUtil.ACCESS_TOKEN_SECRET));
}
@Test
public void isAccessTokenValid() throws Exception {
boolean actual = TwitterUtil.isAccessTokenValid(tester.request);
assertThat(actual, is(true));
}
}
public static class WhenHasSession extends ControllerTestCase {
@Before
public void before() {
SessionUtil.put(tester.request, SessionKey.ACCESS_TOKEN, TestUtil.getMockAccessToken());
}
@Test
public void isAccessTokenValid() throws Exception {
boolean actual = TwitterUtil.isAccessTokenValid(tester.request);
assertThat(actual, is(true));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment