Index: /applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListener.java
===================================================================
--- /applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListener.java	(revision 31485)
+++ /applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListener.java	(revision 31486)
@@ -19,4 +19,6 @@
  */
 public class OAuthPortListener extends Thread {
+
+  protected static String RESPONSE = "<html><head><title>Mapillary login</title></head><body>Login successful, return to JOSM.</body></html>";
 
   @Override
@@ -58,7 +60,8 @@
           + accessToken);
       // Saves the access token in preferences.
-      Main.pref.put("mapillary.access-token", accessToken);
-
-      Main.info("The username is: " + MapillaryUser.getUsername());
+      if (Main.main != null) {
+        Main.pref.put("mapillary.access-token", accessToken);
+        Main.info("The username is: " + MapillaryUser.getUsername());
+      }
 
     } catch (BindException e) {
@@ -70,9 +73,8 @@
 
   private static void writeContent(PrintWriter out) {
-    String response = "<html><head><title>Mapillary login</title></head><body>Login successful, return to JOSM.</body></html>";
     out.println("HTTP/1.1 200 OK");
-    out.println("Content-Length: " + response.length());
+    out.println("Content-Length: " + RESPONSE.length());
     out.println("Content-Type: text/html" + "\r\n\r\n");
-    out.println(response);
+    out.println(RESPONSE);
   }
 }
Index: /applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListenerTest.java
===================================================================
--- /applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListenerTest.java	(revision 31486)
+++ /applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListenerTest.java	(revision 31486)
@@ -0,0 +1,40 @@
+package org.openstreetmap.josm.plugins.mapillary.oauth;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import org.junit.Test;
+
+/**
+ * Tests the {@link OAuthPortListener} class.
+ *
+ * @author nokutu
+ * @see OAuthPortListener
+ *
+ */
+public class OAuthPortListenerTest {
+
+  /**
+   * Test that the threads responds when the browser makes the request.
+   */
+  @Test
+  public void responseTest() {
+    OAuthPortListener t = new OAuthPortListener();
+    t.start();
+    try {
+      URL url = new URL("http://localhost:8763?access_token=access_token");
+      HttpURLConnection con = (HttpURLConnection) url.openConnection();
+      BufferedReader in = new BufferedReader(new InputStreamReader(
+          con.getInputStream()));
+      in.readLine();
+      assertEquals(OAuthPortListener.RESPONSE, in.readLine());
+    } catch (Exception e) {
+      fail();
+    }
+  }
+}
