Skip to content

Commit 6786462

Browse files
author
coursar
committed
feat(params)
1 parent 2299516 commit 6786462

File tree

8 files changed

+175
-1
lines changed

8 files changed

+175
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
2.2. [x] [Системы сборки и авто-тесты](maven-junit)
1414

15-
2.3. [ ] [Циклы, параметризованные тесты и аннотации](params)
15+
2.3. [x] [Циклы, параметризованные тесты и аннотации](params)
1616

1717
2.4. [ ] [CI/CD/CD](continuous)
1818

params/bonus-calculator/pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ru.netology</groupId>
8+
<artifactId>bonus-calculator</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.junit.jupiter</groupId>
20+
<artifactId>junit-jupiter</artifactId>
21+
<version>5.4.2</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-surefire-plugin</artifactId>
31+
<version>2.22.2</version>
32+
</plugin>
33+
</plugins>
34+
</build>
35+
</project>
36+
37+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.netology.bonus;
2+
3+
public class BonusService {
4+
public long calculate(long amount, boolean registered) {
5+
int percent = registered ? 3 : 1;
6+
long bonus = amount * percent / 100 / 100;
7+
long limit = 500;
8+
if (bonus > limit) {
9+
bonus = limit;
10+
}
11+
return bonus;
12+
}
13+
}
14+
15+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package ru.netology.bonus;
2+
3+
public class Main {
4+
public static void main(String[] args) { }
5+
}
6+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.netology.bonus;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
class BonusServiceTest {
9+
@ParameterizedTest
10+
@CsvSource(
11+
value={
12+
"'registered user, bonus under limit',100060,true,30",
13+
"'registered user, bonus over limit',100000060,true,500"
14+
}
15+
)
16+
void shouldCalculate(String test, long amount, boolean registered, long expected) {
17+
BonusService service = new BonusService();
18+
19+
// вызываем целевой метод:
20+
long actual = service.calculate(amount, registered);
21+
22+
// производим проверку (сравниваем ожидаемый и фактический):
23+
assertEquals(expected, actual);
24+
}
25+
}
26+

params/statistics/pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ru.netology</groupId>
8+
<artifactId>statistics</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.junit.jupiter</groupId>
20+
<artifactId>junit-jupiter</artifactId>
21+
<version>5.4.2</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-surefire-plugin</artifactId>
31+
<version>2.22.2</version>
32+
</plugin>
33+
</plugins>
34+
</build>
35+
</project>
36+
37+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ru.netology.statistic;
2+
3+
public class StatisticsService {
4+
public long calculateSum(long[] purchases) {
5+
long sum = 0;
6+
for (long purchase : purchases) {
7+
// аналог sum = sum + purchase;
8+
sum += purchase;
9+
}
10+
return sum;
11+
}
12+
13+
public long findMax(long[] purchases) {
14+
long currentMax = purchases[0];
15+
for (long purchase : purchases) {
16+
if (currentMax < purchase) {
17+
currentMax = purchase;
18+
}
19+
}
20+
return currentMax;
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ru.netology.statistic;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class StatisticsServiceTest {
8+
@Test
9+
void calculateSum() {
10+
StatisticsService service = new StatisticsService();
11+
12+
long[] purchases = {1_000, 2_000, 500, 5_000, 2_000};
13+
long expected = 10_500;
14+
15+
long actual = service.calculateSum(purchases);
16+
17+
assertEquals(expected, actual);
18+
}
19+
20+
@Test
21+
void findMax() {
22+
StatisticsService service = new StatisticsService();
23+
24+
long[] purchases = {1_000, 2_000, 500, 5_000, 2_000};
25+
long expected = 5_000;
26+
27+
long actual = service.findMax(purchases);
28+
29+
assertEquals(expected, actual);
30+
}
31+
}

0 commit comments

Comments
 (0)