jandersen
jandersen

Reputation: 3561

JUnit Test of Code that uses GAE/J URLFetchServiceFactory.getURLFetchService()

I've got some code I'm deploying to Google App Engine - Java (GAE/J) that makes use of the URLFetchService. I'd like to use JUnit to test this code. Per the testing documentation, it appears I should have a test that uses their LocalURLFetchServiceTestConfig class ROUGHLY as follows:

public class MyRemoteServiceTests {
    private static final LocalURLFetchServiceTestConfig urlConfig = new LocalURLFetchServiceTestConfig();
    private static final LocalServiceTestHelper helper = 
        new LocalServiceTestHelper(urlConfig);
    
    @Before
    public void setUp() throws Exception {
        service = new SampleService();
        helper.setUp();
    }

    @After
    public void tearDown() throws Exception {
        service = null;
        helper.tearDown();
    }

    @Test
    public void testThatCallsCodeThatUsesUrlFetch() {
            Object data = service.getRemoteDataUsingUrlFetch("foo", "bar");
            Assert.assertNotNull(data);
    }

}

I'm finding that this test continues to fail despite using the "helper" as suggested in the GAE/J documentation on testing: "The API package 'urlfetch' or call 'Fetch()' was not found.".

I was assuming that using the "helper" would somehow setup the GAE environment such that when I call URLFetchServiceFactory.getURLFetchService() from within my getRemoteDataUsingUrlFetch method, the interface returned would be an instance of LocalURLFetchService that would just "work" but that seems NOT to be the case.

Any help/suggestions much appreciated!

Upvotes: 2

Views: 1019

Answers (2)

jandersen
jandersen

Reputation: 3561

Actually, it turns out my problem was failure to include two additional jars that ARE mentioned on the Local Unit Testing page of the documentation. RTM FTW!

  • appengine-local-runtime.jar
  • appengine-api-stubs.jar

Upvotes: 2

aldrin
aldrin

Reputation: 4572

afaik, the LocalURLFetchService doesn't configure the GAE like you expect. It is more of a way to fetch URL from the local dev and then process the contents however. (Similarly even the LocalDatastoreService and LocalMemcacheService operate on isolated spaces within the test environment) One way to test your code is to refactor the getRemoteDataUsingUrlFetch() to take maybe the contents of the Url response. somewhat like,

URLFetchResponse resp = LocalURLFetchService.fetch(status, request)

getRemoteDataUsingUrlFetch(foo, bar, resp)

Upvotes: 0

Related Questions