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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Compiled class file
*.class
.idea/
*.iml
target/

# Log file
*.log
Expand Down
54 changes: 49 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>untitled</artifactId>
<groupId>com.example</groupId>
<artifactId>animal-project</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</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>4.11.0</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>

<!-- Compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand All @@ -25,6 +47,28 @@
<target>11</target>
</configuration>
</plugin>

<!-- JaCoCo -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/example/Lion.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@

public class Lion {

boolean hasMane;
private final boolean hasMane;
private final Feline feline;

public Lion(String sex, Feline feline) throws Exception {

this.feline = feline;

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

Feline feline = new Feline();

public int getKittens() {
return feline.getKittens();
}
Expand All @@ -29,4 +31,4 @@ public boolean doesHaveMane() {
public List<String> getFood() throws Exception {
return feline.getFood("Хищник");
}
}
}
33 changes: 33 additions & 0 deletions src/test/java/com/example/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.List;

public class CatTest {

@Test
public void getSoundTest() {
Feline feline = Mockito.mock(Feline.class);
Cat cat = new Cat(feline);

Assert.assertEquals("Мяу", cat.getSound());
}

@Test
public void getFoodTest() throws Exception {
Feline feline = Mockito.mock(Feline.class);

Mockito.when(feline.eatMeat())
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛔️Нужно исправить. Мок должен вести себя как реальный объект и возвращать List.of("Животные", "Птицы", "Рыба");

.thenReturn(List.of("Животные", "Птицы", "Рыба"));

Cat cat = new Cat(feline);

Assert.assertEquals(
List.of("Животные", "Птицы", "Рыба"),
cat.getFood()
);
}
}
42 changes: 42 additions & 0 deletions src/test/java/com/example/FelineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example;

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

import java.util.List;

public class FelineTest {

private Feline feline;

@Before
public void setUp() {
feline = new Feline();
}

@Test
public void eatMeatTest() throws Exception {
List<String> food = feline.eatMeat();

Assert.assertEquals(
List.of("Животные", "Птицы", "Рыба"),
food
);
}

@Test
public void getFamilyTest() {
Assert.assertEquals("Кошачьи", feline.getFamily());
}

@Test
public void getKittensDefaultTest() {
Assert.assertEquals(1, feline.getKittens());
}

@Test
public void getKittensWithParameterTest() {
Assert.assertEquals(5, feline.getKittens(5));
}
}
43 changes: 43 additions & 0 deletions src/test/java/com/example/LionParameterizedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.Mockito;

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

@RunWith(Parameterized.class)
public class LionParameterizedTest {

private final String sex;
private final boolean expectedHasMane;

public LionParameterizedTest(String sex, boolean expectedHasMane) {
this.sex = sex;
this.expectedHasMane = expectedHasMane;
}

@Parameterized.Parameters(name = "Пол: {0} → есть грива: {1}")
public static Collection<Object[]> data() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛔️Нужно исправить. Вижу только один тест для класса lion. Проверь, пожалуйста, пулл реквест

return Arrays.asList(new Object[][]{
{"Самец", true},
{"Самка", false}
});
}

@Test
public void doesHaveManeTest() throws Exception {

Feline mockFeline = Mockito.mock(Feline.class);

Lion lion = new Lion(sex, mockFeline);

Assert.assertEquals(
expectedHasMane,
lion.doesHaveMane()
);
}
}
43 changes: 43 additions & 0 deletions src/test/java/com/example/LionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example;

import org.junit.Test;
import org.mockito.Mockito;

import java.util.List;

import static org.junit.Assert.assertEquals;

public class LionTest {

@Test
public void getKittensTest() throws Exception {
Feline feline = Mockito.mock(Feline.class);
Mockito.when(feline.getKittens()).thenReturn(3);

Lion lion = new Lion("Самец", feline);

assertEquals(3, lion.getKittens());
}

@Test
public void getFoodTest() throws Exception {
Feline feline = Mockito.mock(Feline.class);

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

Lion lion = new Lion("Самка", feline);

assertEquals(
List.of("Животные", "Птицы", "Рыба"),
lion.getFood()
);
}

@Test(expected = Exception.class)
public void constructorExceptionTest() throws Exception {
Feline feline = Mockito.mock(Feline.class);

new Lion("Неизвестно", feline);
}
}
Binary file added target/jacoco.exec
Binary file not shown.
1 change: 1 addition & 0 deletions target/site/jacoco/com.example/Animal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="ru"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>Animal</title><script type="text/javascript" src="../jacoco-resources/sort.js"></script></head><body onload="initialSort(['breadcrumb'])"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">animal-project</a> &gt; <a href="index.html" class="el_package">com.example</a> &gt; <span class="el_class">Animal</span></div><h1>Animal</h1><table class="coverage" cellspacing="0" id="coveragetable"><thead><tr><td class="sortable" id="a" onclick="toggleSort(this)">Element</td><td class="down sortable bar" id="b" onclick="toggleSort(this)">Missed Instructions</td><td class="sortable ctr2" id="c" onclick="toggleSort(this)">Cov.</td><td class="sortable bar" id="d" onclick="toggleSort(this)">Missed Branches</td><td class="sortable ctr2" id="e" onclick="toggleSort(this)">Cov.</td><td class="sortable ctr1" id="f" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="g" onclick="toggleSort(this)">Cxty</td><td class="sortable ctr1" id="h" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="i" onclick="toggleSort(this)">Lines</td><td class="sortable ctr1" id="j" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="k" onclick="toggleSort(this)">Methods</td></tr></thead><tfoot><tr><td>Total</td><td class="bar">11 of 27</td><td class="ctr2">59 %</td><td class="bar">2 of 4</td><td class="ctr2">50 %</td><td class="ctr1">3</td><td class="ctr2">5</td><td class="ctr1">3</td><td class="ctr2">7</td><td class="ctr1">1</td><td class="ctr2">3</td></tr></tfoot><tbody><tr><td id="a2"><a href="Animal.java.html#L8" class="el_method">getFood(String)</a></td><td class="bar" id="b0"><img src="../jacoco-resources/redbar.gif" width="49" height="10" title="9" alt="9"/><img src="../jacoco-resources/greenbar.gif" width="70" height="10" title="13" alt="13"/></td><td class="ctr2" id="c1">59 %</td><td class="bar" id="d0"><img src="../jacoco-resources/redbar.gif" width="60" height="10" title="2" alt="2"/><img src="../jacoco-resources/greenbar.gif" width="60" height="10" title="2" alt="2"/></td><td class="ctr2" id="e0">50 %</td><td class="ctr1" id="f0">2</td><td class="ctr2" id="g0">3</td><td class="ctr1" id="h0">2</td><td class="ctr2" id="i0">5</td><td class="ctr1" id="j1">0</td><td class="ctr2" id="k0">1</td></tr><tr><td id="a1"><a href="Animal.java.html#L18" class="el_method">getFamily()</a></td><td class="bar" id="b1"><img src="../jacoco-resources/redbar.gif" width="10" height="10" title="2" alt="2"/></td><td class="ctr2" id="c2">0 %</td><td class="bar" id="d1"/><td class="ctr2" id="e1">n/a</td><td class="ctr1" id="f1">1</td><td class="ctr2" id="g1">1</td><td class="ctr1" id="h1">1</td><td class="ctr2" id="i1">1</td><td class="ctr1" id="j0">1</td><td class="ctr2" id="k1">1</td></tr><tr><td id="a0"><a href="Animal.java.html#L5" class="el_method">Animal()</a></td><td class="bar" id="b2"><img src="../jacoco-resources/greenbar.gif" width="16" height="10" title="3" alt="3"/></td><td class="ctr2" id="c0">100 %</td><td class="bar" id="d2"/><td class="ctr2" id="e2">n/a</td><td class="ctr1" id="f2">0</td><td class="ctr2" id="g2">1</td><td class="ctr1" id="h2">0</td><td class="ctr2" id="i2">1</td><td class="ctr1" id="j2">0</td><td class="ctr2" id="k2">1</td></tr></tbody></table><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.8.202204050719</span></div></body></html>
21 changes: 21 additions & 0 deletions target/site/jacoco/com.example/Animal.java.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="ru"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>Animal.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">animal-project</a> &gt; <a href="index.source.html" class="el_package">com.example</a> &gt; <span class="el_source">Animal.java</span></div><h1>Animal.java</h1><pre class="source lang-java linenums">package com.example;

import java.util.List;

<span class="fc" id="L5">public class Animal {</span>

public List&lt;String&gt; getFood(String animalKind) throws Exception {
<span class="pc bpc" id="L8" title="1 of 2 branches missed."> if (&quot;Травоядное&quot;.equals(animalKind)) {</span>
<span class="nc" id="L9"> return List.of(&quot;Трава&quot;, &quot;Различные растения&quot;);</span>
<span class="pc bpc" id="L10" title="1 of 2 branches missed."> } else if (&quot;Хищник&quot;.equals(animalKind)) {</span>
<span class="fc" id="L11"> return List.of(&quot;Животные&quot;, &quot;Птицы&quot;, &quot;Рыба&quot;);</span>
} else {
<span class="nc" id="L13"> throw new Exception(&quot;Неизвестный вид животного, используйте значение Травоядное или Хищник&quot;);</span>
}
}

public String getFamily() {
<span class="nc" id="L18"> return &quot;Существует несколько семейств: заячьи, беличьи, мышиные, кошачьи, псовые, медвежьи, куньи&quot;;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.8.202204050719</span></div></body></html>
1 change: 1 addition & 0 deletions target/site/jacoco/com.example/Cat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="ru"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>Cat</title><script type="text/javascript" src="../jacoco-resources/sort.js"></script></head><body onload="initialSort(['breadcrumb'])"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">animal-project</a> &gt; <a href="index.html" class="el_package">com.example</a> &gt; <span class="el_class">Cat</span></div><h1>Cat</h1><table class="coverage" cellspacing="0" id="coveragetable"><thead><tr><td class="sortable" id="a" onclick="toggleSort(this)">Element</td><td class="down sortable bar" id="b" onclick="toggleSort(this)">Missed Instructions</td><td class="sortable ctr2" id="c" onclick="toggleSort(this)">Cov.</td><td class="sortable bar" id="d" onclick="toggleSort(this)">Missed Branches</td><td class="sortable ctr2" id="e" onclick="toggleSort(this)">Cov.</td><td class="sortable ctr1" id="f" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="g" onclick="toggleSort(this)">Cxty</td><td class="sortable ctr1" id="h" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="i" onclick="toggleSort(this)">Lines</td><td class="sortable ctr1" id="j" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="k" onclick="toggleSort(this)">Methods</td></tr></thead><tfoot><tr><td>Total</td><td class="bar">0 of 12</td><td class="ctr2">100 %</td><td class="bar">0 of 0</td><td class="ctr2">n/a</td><td class="ctr1">0</td><td class="ctr2">3</td><td class="ctr1">0</td><td class="ctr2">5</td><td class="ctr1">0</td><td class="ctr2">3</td></tr></tfoot><tbody><tr><td id="a0"><a href="Cat.java.html#L9" class="el_method">Cat(Feline)</a></td><td class="bar" id="b0"><img src="../jacoco-resources/greenbar.gif" width="120" height="10" title="6" alt="6"/></td><td class="ctr2" id="c0">100 %</td><td class="bar" id="d0"/><td class="ctr2" id="e0">n/a</td><td class="ctr1" id="f0">0</td><td class="ctr2" id="g0">1</td><td class="ctr1" id="h0">0</td><td class="ctr2" id="i0">3</td><td class="ctr1" id="j0">0</td><td class="ctr2" id="k0">1</td></tr><tr><td id="a1"><a href="Cat.java.html#L18" class="el_method">getFood()</a></td><td class="bar" id="b1"><img src="../jacoco-resources/greenbar.gif" width="80" height="10" title="4" alt="4"/></td><td class="ctr2" id="c1">100 %</td><td class="bar" id="d1"/><td class="ctr2" id="e1">n/a</td><td class="ctr1" id="f1">0</td><td class="ctr2" id="g1">1</td><td class="ctr1" id="h1">0</td><td class="ctr2" id="i1">1</td><td class="ctr1" id="j1">0</td><td class="ctr2" id="k1">1</td></tr><tr><td id="a2"><a href="Cat.java.html#L14" class="el_method">getSound()</a></td><td class="bar" id="b2"><img src="../jacoco-resources/greenbar.gif" width="40" height="10" title="2" alt="2"/></td><td class="ctr2" id="c2">100 %</td><td class="bar" id="d2"/><td class="ctr2" id="e2">n/a</td><td class="ctr1" id="f2">0</td><td class="ctr2" id="g2">1</td><td class="ctr1" id="h2">0</td><td class="ctr2" id="i2">1</td><td class="ctr1" id="j2">0</td><td class="ctr2" id="k2">1</td></tr></tbody></table><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.8.202204050719</span></div></body></html>
22 changes: 22 additions & 0 deletions target/site/jacoco/com.example/Cat.java.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="ru"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>Cat.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">animal-project</a> &gt; <a href="index.source.html" class="el_package">com.example</a> &gt; <span class="el_source">Cat.java</span></div><h1>Cat.java</h1><pre class="source lang-java linenums">package com.example;

import java.util.List;

public class Cat {

Predator predator;

<span class="fc" id="L9"> public Cat(Feline feline) {</span>
<span class="fc" id="L10"> this.predator = feline;</span>
<span class="fc" id="L11"> }</span>

public String getSound() {
<span class="fc" id="L14"> return &quot;Мяу&quot;;</span>
}

public List&lt;String&gt; getFood() throws Exception {
<span class="fc" id="L18"> return predator.eatMeat();</span>
}

}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.8.202204050719</span></div></body></html>
Loading