Index: /trunk/test/unit/org/openstreetmap/josm/actions/JoinAreasActionTest.java
===================================================================
--- /trunk/test/unit/org/openstreetmap/josm/actions/JoinAreasActionTest.java	(revision 9662)
+++ /trunk/test/unit/org/openstreetmap/josm/actions/JoinAreasActionTest.java	(revision 9663)
@@ -14,4 +14,5 @@
 import org.openstreetmap.josm.actions.search.SearchAction;
 import org.openstreetmap.josm.data.osm.DataSet;
+import org.openstreetmap.josm.gui.layer.Layer;
 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
 import org.openstreetmap.josm.io.IllegalDataException;
@@ -40,7 +41,13 @@
         try (InputStream is = TestUtils.getRegressionDataStream(10511, "10511_mini.osm")) {
             DataSet ds = OsmReader.parseDataSet(is, null);
-            Main.map.mapView.addLayer(new OsmDataLayer(ds, null, null));
+            Layer layer = new OsmDataLayer(ds, null, null);
+            Main.main.addLayer(layer);
             // FIXME enable this test after we fix the bug. Test disabled for now
-            // new JoinAreasAction().join(ds.getWays());
+            // try {
+            //     new JoinAreasAction().join(ds.getWays());
+            // } finally {
+            // Ensure we clean the place before leaving, even if test fails.
+            Main.main.removeLayer(layer);
+            // }
         }
     }
@@ -56,12 +63,18 @@
             DataSet ds = OsmReader.parseDataSet(is, null);
             assertEquals(10, ds.getWays().size());
-            Main.map.mapView.addLayer(new OsmDataLayer(ds, null, null));
-            for (String ref : new String[]{"A", "B", "C", "D", "E"}) {
-                System.out.print("Joining ways " + ref);
-                SearchAction.search("type:way ref="+ref, SearchAction.SearchMode.replace);
-                assertEquals(2, ds.getSelectedWays().size());
-                Main.main.menu.joinAreas.join(ds.getSelectedWays());
-                assertEquals(1, ds.getSelectedWays().size());
-                System.out.println(" ==> OK");
+            Layer layer = new OsmDataLayer(ds, null, null);
+            Main.main.addLayer(layer);
+            try {
+                for (String ref : new String[]{"A", "B", "C", "D", "E"}) {
+                    System.out.print("Joining ways " + ref);
+                    SearchAction.search("type:way ref="+ref, SearchAction.SearchMode.replace);
+                    assertEquals(2, ds.getSelectedWays().size());
+                    Main.main.menu.joinAreas.join(ds.getSelectedWays());
+                    assertEquals(1, ds.getSelectedWays().size());
+                    System.out.println(" ==> OK");
+                }
+            } finally {
+                // Ensure we clean the place before leaving, even if test fails.
+                Main.main.removeLayer(layer);
             }
         }
