Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 45 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,30 @@
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<jacoco.version>0.8.7</jacoco.version>
</properties>

<dependencies>

<!-- JUnit 4 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

<!-- Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -25,6 +45,30 @@
<target>11</target>
</configuration>
</plugin>

<!-- JaCoCo -->

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>prepare-agent</id>
<phase>initialize</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/example/Alex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example;

import java.util.List;

public class Alex extends Lion{

public Alex(Feline feline) throws Exception {
super("Самец", feline);
}

public List<String> getFriends() {
return List.of("Марти", "Глория", "Мелман");
}

public String getPlaceOfLiving() {
return "Нью-Йоркский зоопарк";
}

@Override
public int getKittens() {
return 0;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/example/Cat.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ public List<String> getFood() throws Exception {
return predator.eatMeat();
}

}
}
2 changes: 1 addition & 1 deletion src/main/java/com/example/Feline.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ public int getKittens(int kittensCount) {
return kittensCount;
}

}
}
7 changes: 4 additions & 3 deletions src/main/java/com/example/Lion.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
public class Lion {

boolean hasMane;
Feline feline;

public Lion(String sex) throws Exception {
public Lion(String sex, Feline feline) throws Exception {
if ("Самец".equals(sex)) {
hasMane = true;
} else if ("Самка".equals(sex)) {
hasMane = false;
} else {
throw new Exception("Используйте допустимые значения пола животного - самей или самка");
throw new Exception("Используйте допустимые значения пола животного - самец или самка");
}
this.feline = feline;
}

Feline feline = new Feline();

public int getKittens() {
return feline.getKittens();
Expand Down
83 changes: 83 additions & 0 deletions src/test/java/com/example/AlexTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.example;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.List;

import static org.junit.Assert.*;

@RunWith(MockitoJUnitRunner.class)
public class AlexTest {

@Mock
private Feline feline;
private Alex alex;

@Before
public void setUp() throws Exception {
alex = new Alex(feline);
}
@Test
public void shouldCreateAlexSuccessfullyAndHaveMane() throws Exception {
boolean hasMane = alex.doesHaveMane();

assertTrue(hasMane);
}

@Test
public void shouldReturnFriendsList() throws Exception {

List<String> friends = alex.getFriends();

assertEquals(List.of("Марти", "Глория", "Мелман"), friends);
}

@Test
public void shouldReturnPlaceOfLiving() throws Exception {

String place = alex.getPlaceOfLiving();

assertEquals("Нью-Йоркский зоопарк", place);
}

@Test
public void shouldReturnZeroKittens() throws Exception {
int kittens = alex.getKittens();

assertEquals(0, kittens);
}

@Test
public void shouldNotInteractWithFelineWhenGetKittensCalled() throws Exception {

alex.getKittens();

Mockito.verifyNoInteractions(feline);
}

@Test
public void shouldReturnFoodList() throws Exception {

List<String> expectedFood = List.of("Животные", "Птицы", "Рыба");
Mockito.when(feline.getFood("Хищник")).thenReturn(expectedFood);

List<String> food = alex.getFood();

assertEquals(expectedFood, food);
}

@Test
public void shouldCallFelineGetFoodWithPredator() throws Exception {

Mockito.when(feline.getFood("Хищник")).thenReturn(List.of("Животные"));

alex.getFood();

Mockito.verify(feline).getFood("Хищник");
}
}
41 changes: 41 additions & 0 deletions src/test/java/com/example/AnimalParametrizedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static org.junit.Assert.*;

@RunWith(Parameterized.class)
public class AnimalParametrizedTest {

private final String animalKind;

private final List<String> expectedFood;

@Parameterized.Parameters(name = "animalKind={0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"Травоядное", List.of("Трава", "Различные растения")},
{"Хищник", List.of("Животные", "Птицы", "Рыба")}
});
}

public AnimalParametrizedTest(String animalKind, List<String> expectedFood) {
this.animalKind = animalKind;
this.expectedFood = expectedFood;
}

@Test
public void shouldReturnFoodWhenAnimalKindIsValid() throws Exception {
Animal animal = new Animal();

List<String> actualFood = animal.getFood(animalKind);

assertEquals(expectedFood, actualFood);
}
}
35 changes: 35 additions & 0 deletions src/test/java/com/example/AnimalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class AnimalTest {

private Animal animal;

@Before
public void setUp() {
animal = new Animal();
}

@Test(expected = Exception.class)
public void shouldThrowExceptionWhenAnimalKindIsUnknown() throws Exception {
String unknownKind = "Птица";

animal.getFood(unknownKind);

}

@Test
public void shouldReturnFamilyWhenGetFamilyCalled() {

String family = animal.getFamily();

assertEquals(
"Существует несколько семейств: заячьи, беличьи, мышиные, кошачьи, псовые, медвежьи, куньи",
family
);
}
}
Loading