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
227 changes: 122 additions & 105 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,115 +1,132 @@
<?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">
<modelVersion>4.0.0</modelVersion>
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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.dmdev</groupId>
<artifactId>junit5-trainer</artifactId>
<version>1.0-SNAPSHOT</version>
<groupId>com.dmdev</groupId>
<artifactId>junit5-trainer</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<lombok.version>1.18.22</lombok.version>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<lombok.version>1.18.22</lombok.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.3</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.2</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.210</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.26.3</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.14.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.220</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-wrapper-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M5</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>generate-jacoco-report</id>
<goals>
<goal>report</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-wrapper-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M5</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>generate-jacoco-report</id>
<goals>
<goal>report</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
</project>
142 changes: 142 additions & 0 deletions src/test/java/com/dmdev/dao/SubscriptionDaoIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.dmdev.dao;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.dmdev.entity.Provider;
import com.dmdev.entity.Status;
import com.dmdev.entity.Subscription;
import com.dmdev.integration.IntegrationTestBase;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;

class SubscriptionDaoIT extends IntegrationTestBase {

private final SubscriptionDao subscriptionDao = SubscriptionDao.getInstance();

@Test
void findAll() {
var subscriptionFirst = subscriptionDao.insert(getSubscription("first"));
var subscriptionSecond = subscriptionDao.insert(getSubscription("second "));
var subscriptionThird = subscriptionDao.insert(getSubscription("third"));

List<Subscription> result = subscriptionDao.findAll();

assertEquals(List.of(subscriptionFirst, subscriptionSecond, subscriptionThird), result);

List<Integer> ids = result.stream()
.map(Subscription::getId)
.toList();
assertThat(ids).contains(subscriptionFirst.getId(), subscriptionSecond.getId(),
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

нет смысла в этом ассерте, когда есть assertEquals такой чуть выше

subscriptionThird.getId());
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Разделяй четко 3 секции в тесте, чтобы улучшить его читабильность: given/when/then



@Test
void findByIdWhenIdExistsThenReturnSubscription() {
var subscriptionFirst = subscriptionDao.insert(getSubscription("first"));

var result = subscriptionDao.findById(subscriptionFirst.getId());

assertNotNull(result);
assertEquals(Optional.of(subscriptionFirst), result);
}

@Test
void findByIdWhenIdNotExistsThenReturnOptionalEmpty() {
var result = subscriptionDao.findById(3212);

assertEquals(Optional.empty(), result);
}

@Test
void deleteWhenIdExistsThenReturnTrue() {
var subscriptionFirst = subscriptionDao.insert(getSubscription("first"));

var result = subscriptionDao.delete(subscriptionFirst.getId());

assertTrue(result);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

не надо делать отступов в конце метода

}

@Test
void deleteWhenIdNotExistsThenReturnFalse() {
subscriptionDao.insert(getSubscription("first"));

var result = subscriptionDao.delete(1234);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

нужно выбирать более нереалистичное значение id
1234 легко достичь в тестах, что приведет к его flaky


assertFalse(result);

}

@Test
void update() {
var subscription = getSubscription("first");
subscriptionDao.insert(subscription);
subscription.setName("second");
subscription.setProvider(Provider.GOOGLE);

var result = subscriptionDao.update(subscription);

assertNotNull(result);
assertEquals(subscription, result);

}

@Test
void insert() {
var subscription = getSubscription("first");

var result = subscriptionDao.insert(subscription);

assertNotNull(result);
}

@Test
void findByUserIdWhenIdExistsThenReturnSubscriptions() {
var userid = 123;
var subscriptionFirst = subscriptionDao.insert(getSubscription("first").setUserId(userid));
var subscriptionSecond = subscriptionDao.insert(getSubscription("second").setUserId(userid));

List<Subscription> result = subscriptionDao.findByUserId(userid);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Для теста метода findByUserId нужна вставить несколько подписок с разным userId - иначе некорректный dataset. Его можно заменить на findAll, например, и он все еще будет проходить


assertNotNull(result);
assertEquals(2, result.size());

List<Integer> ids = result.stream()
.map(Subscription::getId)
.toList();
assertThat(ids).contains(subscriptionFirst.getId(), subscriptionSecond.getId());

}

@Test
void findByUserIdWhenIdNotExistsThenReturnEmptyList() {
var userid = 123;
subscriptionDao.insert(getSubscription("first").setUserId(userid));
subscriptionDao.insert(getSubscription("second").setUserId(userid));

var result = subscriptionDao.findByUserId(3212);

assertEquals(Collections.EMPTY_LIST, result);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

проверяется на empty list, а не сравнивается с константой. Ибо это может быть и не константа вернута тебе

}


private Subscription getSubscription(String name) {
return Subscription.builder()
.userId(123)
.name(name)
.provider(Provider.APPLE)
.status(Status.ACTIVE)
.expirationDate(Instant.parse("2025-11-12T10:00:00Z"))
.build();
}


}
Loading