During World War I and World War II, Switzerland maintained armed neutrality, and was not invaded by its neighbors, in part because of its topography, much of which is mountainous. Germany was a threat and Switzerland built a powerful defense. It served as a "protecting power" for the belligerents of both sides, with a special role in helping prisoners of war. The belligerent states made it the scene for diplomacy, espionage, and commerce, as well as being a safe haven for 300,000 refugees.
In The War in the Air - an apocalyptic prediction of the coming global conflict, published in 1908, six years before the actual outbreak of war - H.G. Wells assumed that Switzerland would join the coming war and fight on the side of Germany. Wells is known to have visited Switzerland in 1903, a visit which inspired his book A Modern Utopia, and his assessment of Swiss inclinations might have been inspired by what he heard from Swiss people in that visit.
Switzerland maintained a state of armed neutrality during the first world war. However, with two of the Central Powers (Germany and Austria-Hungary) and two of the Entente Powers (France and Italy) all sharing borders and populations with Switzerland, neutrality proved difficult. Under the Schlieffen Plan, the German General Staff had been open to the possibility of trying to outflank the French fortifications by marching through Switzerland in violation of its neutrality, although the plan's eventual executor Helmuth von Moltke the Younger selected Belgium instead due to Switzerland's mountainous topography and the disorganized state of the Belgian Armed Forces. From December 1914 until the spring of 1918, Swiss troops were deployed in the Jura along the French border over concern that the trench war might spill into Switzerland. Of lesser concern was the Italian border, but troops were also stationed in the Unterengadin region of Graubünden.
While the German-speaking majority in Switzerland generally favored the Central Powers, the French- and, later, Italian-speaking populations sided with the Entente Powers, which would cause internal conflict in 1918.