Add java interface single implementation examples (#13972)
This commit is contained in:
parent
4fb65f5c6e
commit
65f6f416c5
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.interfacesingleimpl;
|
||||
|
||||
public interface Animal {
|
||||
String makeSound();
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.interfacesingleimpl;
|
||||
|
||||
public class AnimalCare {
|
||||
private Animal animal;
|
||||
|
||||
public AnimalCare(Animal animal) {
|
||||
this.animal = animal;
|
||||
}
|
||||
|
||||
public String animalSound() {
|
||||
return animal.makeSound();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.interfacesingleimpl;
|
||||
|
||||
public class Cat {
|
||||
private String name;
|
||||
|
||||
public Cat(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String makeSound() {
|
||||
return "Meow! My name is " + name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.interfacesingleimpl;
|
||||
|
||||
public class Dog implements Animal {
|
||||
private String name;
|
||||
|
||||
public Dog(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String makeSound() {
|
||||
return "Woof! My name is " + name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.interfacesingleimpl;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class InterfaceSingleImplUnitTest {
|
||||
@Test
|
||||
public void whenUsingMockAnimal_thenAnimalSoundIsCorrect() {
|
||||
MockAnimal mockAnimal = new MockAnimal();
|
||||
String expected = "Mock animal sound!";
|
||||
AnimalCare animalCare = new AnimalCare(mockAnimal);
|
||||
assertThat(animalCare.animalSound()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingDog_thenDogMakesWoofSound() {
|
||||
Dog dog = new Dog("Buddy");
|
||||
String expected = "Woof! My name is Buddy";
|
||||
assertThat(dog.makeSound()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingCat_thenCatMakesMeowSound() {
|
||||
Cat cat = new Cat("FuzzBall");
|
||||
String expected = "Meow! My name is FuzzBall";
|
||||
assertThat(cat.makeSound()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingAnimalCareWithDog_thenDogMakesWoofSound() {
|
||||
Animal dog = new Dog("Ham");
|
||||
AnimalCare animalCare = new AnimalCare(dog);
|
||||
String expected = "Woof! My name is Ham";
|
||||
assertThat(animalCare.animalSound()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingCatCareWithCat_thenCatMakesMeowSound() {
|
||||
Cat cat = new Cat("Grumpy");
|
||||
String expected = "Meow! My name is Grumpy";
|
||||
assertThat(cat.makeSound()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingMockAnimal_thenMockAnimalMakesMockAnimalSound() {
|
||||
MockAnimal mockAnimal = new MockAnimal();
|
||||
String expected = "Mock animal sound!";
|
||||
assertThat(mockAnimal.makeSound()).isEqualTo(expected);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.interfacesingleimpl;
|
||||
|
||||
public class MockAnimal implements Animal {
|
||||
@Override
|
||||
public String makeSound() {
|
||||
return "Mock animal sound!";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue