开发者

App Engine Java Unit Test Environment Setup

开发者 https://www.devze.com 2023-04-09 17:29 出处:网络
we use the appengine-web.xml file to store global config dataas system properties (like the location of various development servers we need to communicate with). These properties are not available whe

we use the appengine-web.xml file to store global config data as system properties (like the location of various development servers we need to communicate with). These properties are not available when running unit test with the LocalServiceTestHelper class. There seem to be methods to set the desired system properties during setUp of each unit test (cf. e.g. http://code.google.com/appengine/docs/java/tools/localunittesting/javadoc/com/google/appengine/tools/development/testing开发者_如何学JAVA/LocalServiceTestHelper.html#setEnvAttributes(java.util.Map)) but so far we have failed to implement this.

It would be great if somebody could provide a simple example of how to set a system property in a app engine unit test?

EDIT: here is a minimal example that demonstrates what I am trying to do

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class MyTest {
    private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
@Before
public void setUp() {
    Map<String, Object> values = new HashMap<String, Object>();
    values.put("de.foo.bar", "baz");
    helper.setEnvAttributes(values);
    helper.setUp();
}

@After
public void tearDown() {
    helper.tearDown();
}

@Test
public void foo() {
    System.out.println("my env variable: " + System.getenv("de.foo.bar"));
}

}

This prints out "my env variable: null"


According to the documentation, I think the method setEnvAttributes() does something different than you expect. The environment attributes you're setting are available using Environment.getAttributes() not System.getenv()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号