{"action":"created","issue":{"url":"https://api.github.com/repos/stefanbirkner/system-rules/issues/34","repository_url":"https://api.github.com/repos/stefanbirkner/system-rules","labels_url":"https://api.github.com/repos/stefanbirkner/system-rules/issues/34/labels{/name}","comments_url":"https://api.github.com/repos/stefanbirkner/system-rules/issues/34/comments","events_url":"https://api.github.com/repos/stefanbirkner/system-rules/issues/34/events","html_url":"https://github.com/stefanbirkner/system-rules/issues/34","id":120180668,"number":34,"title":"Add a JUnit Rule to change environment variables","user":{"login":"victornoel","id":160975,"avatar_url":"https://avatars.githubusercontent.com/u/160975?v=3","gravatar_id":"","url":"https://api.github.com/users/victornoel","html_url":"https://github.com/victornoel","followers_url":"https://api.github.com/users/victornoel/followers","following_url":"https://api.github.com/users/victornoel/following{/other_user}","gists_url":"https://api.github.com/users/victornoel/gists{/gist_id}","starred_url":"https://api.github.com/users/victornoel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/victornoel/subscriptions","organizations_url":"https://api.github.com/users/victornoel/orgs","repos_url":"https://api.github.com/users/victornoel/repos","events_url":"https://api.github.com/users/victornoel/events{/privacy}","received_events_url":"https://api.github.com/users/victornoel/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/stefanbirkner/system-rules/milestones/16","html_url":"https://github.com/stefanbirkner/system-rules/milestones/1.16.0","labels_url":"https://api.github.com/repos/stefanbirkner/system-rules/milestones/16/labels","id":1448773,"number":16,"title":"1.16.0","description":"","creator":{"login":"stefanbirkner","id":711349,"avatar_url":"https://avatars.githubusercontent.com/u/711349?v=3","gravatar_id":"","url":"https://api.github.com/users/stefanbirkner","html_url":"https://github.com/stefanbirkner","followers_url":"https://api.github.com/users/stefanbirkner/followers","following_url":"https://api.github.com/users/stefanbirkner/following{/other_user}","gists_url":"https://api.github.com/users/stefanbirkner/gists{/gist_id}","starred_url":"https://api.github.com/users/stefanbirkner/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stefanbirkner/subscriptions","organizations_url":"https://api.github.com/users/stefanbirkner/orgs","repos_url":"https://api.github.com/users/stefanbirkner/repos","events_url":"https://api.github.com/users/stefanbirkner/events{/privacy}","received_events_url":"https://api.github.com/users/stefanbirkner/received_events","type":"User","site_admin":false},"open_issues":0,"closed_issues":2,"state":"closed","created_at":"2015-12-07T22:02:21Z","updated_at":"2016-02-10T22:35:44Z","due_on":null,"closed_at":"2016-02-08T16:04:04Z"},"comments":2,"created_at":"2015-12-03T14:15:49Z","updated_at":"2016-02-10T22:35:44Z","closed_at":"2016-02-10T22:35:44Z","body":"Hi,\r\n\r\nThere is something that is sometimes useful: to add some environment variables to the test environment.\r\n\r\nUsually people configure maven-surefire-plugin to do that, but it's not the best to isolate tests for which it is needed and so on.\r\n\r\nIt would be great to add a JUnit Rule to do that, but it requires some hack as the Java Library does not provide the primitives to do so.\r\n\r\nI wrote one for my tests, but maybe it can be improved and incorporated in system-rules :)\r\n\r\n```java\r\npublic class EnvironmentVariables extends ExternalResource {\r\n\r\n    private final Map<String, String> envVars;\r\n\r\n    private final Map<String, String> extraEnvVars = new HashMap<>();\r\n\r\n    private final Map<String, String> saved = new HashMap<>();\r\n\r\n    private Map<String, String> systemMap;\r\n    \r\n    public EnvironmentVariables(final Map<String, String> envVars) {\r\n        this.envVars = new HashMap<>(envVars);\r\n    }\r\n\r\n    public EnvironmentVariables() {\r\n        this.envVars = new HashMap<>();\r\n    }\r\n\r\n    public void addEnvironmentVariable(final String key, final String value) throws Exception {\r\n        this.extraEnvVars.put(key, value);\r\n\r\n        final String removed = setEnvVar(key, value);\r\n\r\n        // do not save a value that replaces another value that was set in this rule\r\n        if (removed != null && !saved.containsKey(key)) {\r\n            saved.put(key, removed);\r\n        }\r\n    }\r\n\r\n    @SuppressWarnings(\"unchecked\")\r\n    @Override\r\n    protected void before() throws Exception {\r\n\r\n        if (systemMap == null) {\r\n            for (Class<?> cl : Collections.class.getDeclaredClasses()) {\r\n                if (\"java.util.Collections$UnmodifiableMap\".equals(cl.getName())) {\r\n                    final Field field = cl.getDeclaredField(\"m\");\r\n                    field.setAccessible(true);\r\n                    systemMap = (Map<String, String>) field.get(System.getenv());\r\n                    break;\r\n                }\r\n            }\r\n        }\r\n\r\n        Assert.assertNotNull(systemMap);\r\n\r\n        for (Entry<String, String> entry : this.envVars.entrySet()) {\r\n            final String removed = setEnvVar(entry.getKey(), entry.getValue());\r\n            if (removed != null) {\r\n                saved.put(entry.getKey(), removed);\r\n            }\r\n        }\r\n    }\r\n\r\n    @Override\r\n    protected void after() {\r\n        for (Entry<String, String> entry : this.envVars.entrySet()) {\r\n            try {\r\n                setEnvVar(entry.getKey(), null);\r\n            } catch (final Exception e) {\r\n                // do nothing...\r\n            }\r\n        }\r\n        for (Entry<String, String> entry : this.extraEnvVars.entrySet()) {\r\n            try {\r\n                setEnvVar(entry.getKey(), null);\r\n            } catch (final Exception e) {\r\n                // do nothing...\r\n            }\r\n        }\r\n        for (Entry<String, String> entry : this.saved.entrySet()) {\r\n            try {\r\n                setEnvVar(entry.getKey(), entry.getValue());\r\n            } catch (final Exception e) {\r\n                // do nothing\r\n            }\r\n        }\r\n        this.saved.clear();\r\n    }\r\n\r\n    private String setEnvVar(final String key, final String newenv) {\r\n        try {\r\n            if (newenv != null) {\r\n                return systemMap.put(key, newenv);\r\n            } else {\r\n                return systemMap.remove(key);\r\n            }\r\n        } catch (final Exception e) {\r\n            throw new RuntimeException(e);\r\n        }\r\n    }\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/stefanbirkner/system-rules/issues/comments/182611034","html_url":"https://github.com/stefanbirkner/system-rules/issues/34#issuecomment-182611034","issue_url":"https://api.github.com/repos/stefanbirkner/system-rules/issues/34","id":182611034,"user":{"login":"stefanbirkner","id":711349,"avatar_url":"https://avatars.githubusercontent.com/u/711349?v=3","gravatar_id":"","url":"https://api.github.com/users/stefanbirkner","html_url":"https://github.com/stefanbirkner","followers_url":"https://api.github.com/users/stefanbirkner/followers","following_url":"https://api.github.com/users/stefanbirkner/following{/other_user}","gists_url":"https://api.github.com/users/stefanbirkner/gists{/gist_id}","starred_url":"https://api.github.com/users/stefanbirkner/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stefanbirkner/subscriptions","organizations_url":"https://api.github.com/users/stefanbirkner/orgs","repos_url":"https://api.github.com/users/stefanbirkner/repos","events_url":"https://api.github.com/users/stefanbirkner/events{/privacy}","received_events_url":"https://api.github.com/users/stefanbirkner/received_events","type":"User","site_admin":false},"created_at":"2016-02-10T22:35:44Z","updated_at":"2016-02-10T22:35:44Z","body":"System Rules 1.16.0 has a rule `EnvironmentVariables`: e778307cdcd423030a42502342b0cfcc79048655"}}