From 8e305277a73dcb4bed906ede139eed49f32fc29c Mon Sep 17 00:00:00 2001 From: Kai Sellmann <2210892@stud.hs-mannheim.de> Date: Mon, 29 May 2023 20:26:32 +0200 Subject: [PATCH] Test fuer Singleton??? --- .../vererbung/singleton_einfach/Singleton.java | 6 +++--- .../singleton_einfach/SingletonTest.java | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 sources/src/main/java/pr2/vererbung/singleton_einfach/SingletonTest.java diff --git a/sources/src/main/java/pr2/vererbung/singleton_einfach/Singleton.java b/sources/src/main/java/pr2/vererbung/singleton_einfach/Singleton.java index 791a619..eaf2be4 100644 --- a/sources/src/main/java/pr2/vererbung/singleton_einfach/Singleton.java +++ b/sources/src/main/java/pr2/vererbung/singleton_einfach/Singleton.java @@ -2,10 +2,10 @@ package pr2.vererbung.singleton_einfach; public class Singleton { - private String name; + private static String name; Singleton sl = new Singleton("Alleine"); - private Singleton(String name) { + public Singleton(String name) { if (sl == null) { this.setName(name); } else { @@ -16,7 +16,7 @@ package pr2.vererbung.singleton_einfach; return name; } public void setName(String name) { - this.name = name; + Singleton.name = name; } public String getInfo() { diff --git a/sources/src/main/java/pr2/vererbung/singleton_einfach/SingletonTest.java b/sources/src/main/java/pr2/vererbung/singleton_einfach/SingletonTest.java new file mode 100644 index 0000000..2426806 --- /dev/null +++ b/sources/src/main/java/pr2/vererbung/singleton_einfach/SingletonTest.java @@ -0,0 +1,15 @@ +package pr2.vererbung.singleton_einfach; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.Test; + +public class SingletonTest { + + + @Test + public void testVomSingleton() { + Singleton sl = new Singleton("Walter"); + assertEquals("Walter", sl.getName()); + } +}