ИИ — одна из самых обсуждаемых тем последних лет. Многие считают, что он заменит разработчиков, сделав их ненужными. Я решил проверить, насколько это утверждение правдиво, и провести исследование возможностей ИИ в написании кода.
Эта статья — первая из цикла. В рамках исследования я ставлю перед собой несколько задач:
Проверить, насколько качественный и жизнеспособный код генерирует ИИ.
Разобраться, действительно ли ИИ способен заменить разработчиков и можно ли без технических знаний создать работающее приложение.
Сравнить выдачу нескольких LLM (Anthropic Claude 3.5, OpenAI ChatGPT-4o, OpenAI ChatGPT o1-preview, Deepseek R1) и удобство работы с ними.
Поделиться опытом работы с ИИ, используя различные техники промптинга для получения качественных результатов выдачи.
В качестве отправной точки будет подготовлен шаблон проекта Spring Boot.
Стек:
Java 17
Spring Boot с JPA
Liquibase, Hibernate, Lombok
Потребуется реализовать приложение, в котором доступны CRUD-операции:
Создание операции (Operation)
Обновление операции (Operation)
Поиск всех операций (Operation)
Поиск операции по ID (Operation)
Удаление операции (Operation) Также необходимо будет написать unit и MVC тесты на небольшой кусок логики.
Каждой модели ИИ будет дан одинаковый начальный промпт. Затем я буду запрашивать реализацию различных частей функционала. Я не буду бесконечно улучшать код: если он нерабочий, я попрошу исправить ошибки, а если рабочий — отмечу возможные улучшения.
Общение с ИИ будет происходить на английском. Когда я работал с Anthropic Claude впервые, он плохо понимал русский язык, из-за чего качество ответов значительно ухудшалось. Поэтому, чтобы условия были одинаковыми, я буду использовать английский для всех моделей.
Итак, первым испытуемым будет Anthropic Claude 3.5 — или просто Клод.
Весь исходный код доступен на GitHub. Как я уже писал ранее, я создал заготовку проекта с помощью Spring Initializr. Именно её я буду наполнять кодом.
Сначала нужно создать промпт для Клода. Он задаст тон общению и повлияет на качество выдачи.
Роль модели и перечень технологий, с которыми она должна работать.
Цель общения — что я хочу от ИИ.
Правила взаимодействия — например, как он должен отвечать и уточнять вопросы.
You will be acting as a backend developer.
You are have an expertise in the following technologies:
Java 21+, Spring boot, Spring JPA, Hibernate, Lombok, Spring Web, REST API, SQL.
Your goal is to create a production-ready solution for the user and answer
their questions. You should clarify questions to provide the best possible answer.
If you have any questions, ask them first without providing a solution.
Only after all questions have been clarified, you provide a solution for the user.
You should maintain a friendly and professional tone.
Here are some important rules of conduct:
- If you're not sure how to respond, say: "Sorry, I didn't understand you.
Could you please rephrase your question, please?"
- If you don't know the answer to a question, say:
"I am sorry, but I don't know that answer.
Can you please clarify your question for me?"
Here is the user question:
I have already created an application using Spring Initializr with the following dependencies:
Lombok, Spring Web, Spring Data JPA, PostgreSQL Driver, Liquibase Migration.
Write a Liquibase migration for the tables.
<table1>
Category table with columns:
- category_id (bigint),
- category_name (varchar),
- category_description (varchar)
<table1>
<table2>
Operation table with columns:
- operation_id (bigint),
- operation_public_id (varchar),
- operation_name (varchar),
- operation_amount(bigdecimal),
- operation_type (enum),
- category_id (FK to category table)
<table2>
Suggest how to store the operation_type.
In the code, the operation type will be an Enum with the values (DEPOSIT, WITHDRAW).
Think about the answer first before responding. Think step-by-step.
"If you have any questions, ask them first without providing a solution." Эта фраза значительно улучшает ответы модели. Вместо того чтобы сразу выдавать решение, она сначала задаст уточняющие вопросы. Это позволяет добавить детали, которые могли быть упущены.
"Think step-by-step." Фраза заставляет модель лучше продумывать ответ и уменьшает количество ошибок и домыслов.
Помимо базовых инструкций, я также попросил Клода написать миграцию таблиц с использованием Liquibase. Я указал, какие колонки мне нужны, и попросил предложить вариант хранения типов операций.
Промпт готов, первый вопрос задан. Отправляем его Клоду и ждём ответа.
Ответ КлодаLet me analyze your requirements:
Database Structure:
Two tables: Category and Operation
Category is the parent table with a primary key referenced by Operation
Operation has an enum field operation_type
Questions for clarification:
Should category_id in both tables be auto-incrementing?
What should be the maximum lengths for varchar fields (category_name, category_description, operation_public_id, operation_name)?
Should any of these fields be nullable?
For operation_amount, how many digits before and after decimal point do you need?
Should operation_public_id be unique?
Before I provide the Liquibase migration script, please answer these questions so I can create the most appropriate solution for your needs.
Regarding the operation_type storage, I suggest two approaches:
As a VARCHAR field with a check constraint limiting values to 'DEPOSIT' and 'WITHDRAW'
As a custom ENUM type in PostgreSQL
The first approach is generally more flexible for future modifications and better portable across different databases. Would you like me to proceed with this approach, or would you prefer the PostgreSQL ENUM type?
Please provide your preferences so I can create a complete migration script.
Именно поэтому важно давать правильные инструкции. Клод не стал сразу выдавать решение, а уточнил детали. Он проговорил, что ему требуется сделать, и задал вопросы по структуре таблиц (некоторые детали я специально не указывал). Это хороший знак: теперь я могу рассчитывать на корректную миграцию.
Кроме того, Клод предложил несколько вариантов хранения operation_type и объяснил, какой из них предпочитает и почему.
Отвечаем на вопросы Клода и ждём финальный вариант миграции.
1. Correct
2. Questions for clarification:
1. Yes, category_id and operation_id should be auto-incremented
2. For category_name, operation_public_id and operation_name - varchar(255), and for category_description (varchar 1000)
3. Nullable fields: category_name
4. 20 digits before decimal point and 2 after
5. Yes
Regarding the operation_type, let's take the first approach.
Thank you for the clarification. I'll create a Liquibase migration script that meets all these requirements.
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.20.xsd">
<changeSet id="001_create_category_table" author="developer">
<createTable tableName="category">
<column name="category_id" type="bigint">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="category_name" type="varchar(255)">
<constraints nullable="true"/>
</column>
<column name="category_description" type="varchar(1000)">
<constraints nullable="false"/>
</column>
</createTable>
<addAutoIncrement tableName="category" columnName="category_id" columnDataType="bigint" startWith="1" incrementBy="1"/>
</changeSet>
<changeSet id="002_create_operation_table" author="developer">
<createTable tableName="operation">
<column name="operation_id" type="bigint">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="operation_public_id" type="varchar(255)">
<constraints nullable="false" unique="true"/>
</column>
<column name="operation_name" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="operation_amount" type="decimal(22,2)">
<constraints nullable="false"/>
</column>
<column name="operation_type" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="category_id" type="bigint">
<constraints nullable="false"
foreignKeyName="fk_operation_category"
referencedTableName="category"
referencedColumnNames="category_id"/>
</column>
</createTable>
<addAutoIncrement tableName="operation" columnName="operation_id" columnDataType="bigint" startWith="1" incrementBy="1"/>
</changeSet>
<changeSet id="003_add_operation_type_constraint" author="developer">
<sql>
ALTER TABLE operation ADD CONSTRAINT check_operation_type
CHECK (operation_type IN ('DEPOSIT', 'WITHDRAW'));
</sql>
</changeSet>
</databaseChangeLog>
После уточнения деталей он выдал код миграции, но в формате XML. Ни я, ни он не уточнили, в каком формате нужен результат, поэтому я попросил его переделать миграцию используя YAML.
Could you re-write the migration using YAML instead of XML?
Ответ КлодаHere's the equivalent Liquibase migration using YAML format:
databaseChangeLog:
- changeSet:
id: 001_create_category_table
author: developer
changes:
- createTable:
tableName: category
columns:
- column:
name: category_id
type: bigint
constraints:
primaryKey: true
nullable: false
- column:
name: category_name
type: varchar(255)
constraints:
nullable: true
- column:
name: category_description
type: varchar(1000)
constraints:
nullable: false
- addAutoIncrement:
tableName: category
columnName: category_id
columnDataType: bigint
startWith: 1
incrementBy: 1
- changeSet:
id: 002_create_operation_table
author: developer
changes:
- createTable:
tableName: operation
columns:
- column:
name: operation_id
type: bigint
constraints:
primaryKey: true
nullable: false
- column:
name: operation_public_id
type: varchar(255)
constraints:
nullable: false
unique: true
- column:
name: operation_name
type: varchar(255)
constraints:
nullable: false
- column:
name: operation_amount
type: decimal(22,2)
constraints:
nullable: false
- column:
name: operation_type
type: varchar(255)
constraints:
nullable: false
- column:
name: category_id
type: bigint
constraints:
nullable: false
foreignKeyName: fk_operation_category
references: category(category_id)
- addAutoIncrement:
tableName: operation
columnName: operation_id
columnDataType: bigint
startWith: 1
incrementBy: 1
- changeSet:
id: 003_add_operation_type_constraint
author: developer
changes:
- sql:
sql: ALTER TABLE operation ADD CONSTRAINT check_operation_type CHECK (operation_type IN ('DEPOSIT', 'WITHDRAW'));
Как и ожидалось, не всё прошло гладко. Клод не добавил миграцию в databaseChangelogMaster
(и даже не создал этот файл). Ну, с кем не бывает. Я тоже допустил ошибку в промпте: Клод сделал category_name
nullable, а надо было category_description
. Исправлять это я буду вручную.
Я хвалил Клода за уточняющие вопросы перед генерацией миграции, но, кажется, он мог запросить уточняющую информацию о планируемой работе с данными, что могло бы позволить заранее настроить индексы. Однако он этого не сделал, а я, в рамках эксперимента, решил "не знать" об индексах и игнорировать этот момент.
Миграция готова, но прежде чем её запустить, нужно настроить соединение с базой данных. Просим Клода помочь.
Write the configuration to connect the application to the PostgreSQL database.
Ответ КлодаI'll help you configure the database connection. Let me write the configuration for both development and production environments using YAML format.
# src/main/resources/application.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/your_database_name
username: your_username
password: your_password
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: validate # validates the schema, makes no changes to the database
show-sql: false # set to true if you want to see SQL queries in logs
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true # formats SQL queries in logs if show-sql is true
liquibase:
change-log: classpath:db/changelog/db.changelog-master.yaml
enabled: true # enables Liquibase auto-deployment of changes
# Connection pool properties (using HikariCP - Spring Boot default)
hikari:
minimum-idle: 5
maximum-pool-size: 20
idle-timeout: 300000 # 5 minutes
pool-name: HikariPool
max-lifetime: 1200000 # 20 minutes
connection-timeout: 20000 # 20 seconds
For different environments, you can create separate configuration files:
# src/main/resources/application-dev.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/dev_database
username: dev_user
password: dev_password
jpa:
show-sql: true
# src/main/resources/application-prod.yml
spring:
datasource:
url: jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
jpa:
show-sql: false
Клод предложил настроить несколько профилей, что, возможно, избыточно на старте, но полезно для production-ready кода. Также он добавил базовый connection-pool и дал рекомендации по настройке продового окружения.
Но не обошлось без проблем: Клод неправильно объявил connection-pool в application.yaml
, из-за чего Hikari не заработает. Ошибка не критическая, но и неочевидная — Spring просто выдаст предупреждение в логе при запуске. Такие баги самые неприятные, потому что они не приводят к явному падению, но могут сказаться на работе приложения позже.
Вывод: Всегда проверяйте настройки, предложенные ИИ. Он не гарантирует работоспособность кода.
Правильный вариант:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/anthropic_claude?currentSchema=anthropic_claude
username: anthropic_claude_app
password: strongPassword
driver-class-name: org.postgresql.Driver
# Connection pool properties (using HikariCP - Spring Boot default)
hikari:
minimum-idle: 5
maximum-pool-size: 20
idle-timeout: 300000 # 5 minutes
pool-name: HikariPool
max-lifetime: 1200000 # 20 minutes
connection-timeout: 20000 # 20 seconds
Следующий шаг — создание init.sql для удобства локальной разработки. Я попросил Клода создать файл с настройкой прав доступа.
Write init.sql with the following information:
- Create an "app" role with a login password of 'strongPassword'.
- Create a schema "my_app_schema" and authorize the "app" role to use it.
- Grant all privileges to the "app" role on the "my_app_schema"
Ответ КлодаHere's the init.sql script that sets up the database role, schema, and permissions:
-- Create role with login capability
CREATE ROLE app WITH
LOGIN
PASSWORD 'strongPassword'
NOSUPERUSER
INHERIT
NOCREATEDB
NOCREATEROLE
NOREPLICATION;
-- Create schema
CREATE SCHEMA my_app_schema;
-- Grant usage on schema to app role
GRANT USAGE ON SCHEMA my_app_schema TO app;
-- Grant all privileges on all tables in schema to app role
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA my_app_schema TO app;
-- Grant all privileges on all sequences in schema to app role
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA my_app_schema TO app;
-- Make sure new tables and sequences will be accessible by app role
ALTER DEFAULT PRIVILEGES IN SCHEMA my_app_schema
GRANT ALL PRIVILEGES ON TABLES TO app;
ALTER DEFAULT PRIVILEGES IN SCHEMA my_app_schema
GRANT ALL PRIVILEGES ON SEQUENCES TO app;
-- Set search path for the app role
ALTER ROLE app SET search_path TO my_app_schema;
Now you should update your application.yml to use this schema:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/your_database_name?currentSchema=my_app_schema
username: app
password: strongPassword
Клод сделал всё правильно: создал ограниченную роль для приложения и напомнил, что нужно обновить application.yaml
. Это приятно.
Теперь нужно поднять PostgreSQL в Docker. Я решил не тратить время и написал docker-compose.yaml
вручную:
version: '3.1'
services:
anthropic-claude-db:
container_name: anthropic-claude-postgres
image: postgres:15
restart: always
environment:
POSTGRES_USER: anthropic_claude_user
POSTGRES_PASSWORD: strongUserPassword
POSTGRES_DB: anthropic_claude
volumes:
- ./db-volume:/var/lib/postgresql
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- "5432:5432"
Попробуем запустить.
Ожидаемо, Liquibase не смог найти db.changelog-master.yaml
(Liquibase failed to start because no changelog could be found at 'classpath:/db/changelog/db.changelog-master.yaml
). Почему? Потому что Клод забыл его создать. Пишу ему об этом и прошу исправить.
When I start the application, I get an error.
Liquibase failed to start because no changelog could be found at
'classpath:/db/changelog/db.changelog-master.yaml'.
Ответ КлодаYou need to create the changelog directory structure and the master changelog file. Here's what you need to do:
Create the following directory structure in your project:
src/
└── main/
└── resources/
└── db/
└── changelog/
├── db.changelog-master.yaml
└── changes/
└── 001-initial-schema.yaml
Create the master changelog file (db.changelog-master.yaml
):
databaseChangeLog:
- include:
file: changes/001-initial-schema.yaml
relativeToChangelogFile: true
Move the migration we created earlier into 001-initial-schema.yaml
under the changes directory.
Here's the full command sequence if you're using a Unix-like system:
mkdir -p src/main/resources/db/changelog/changes
touch src/main/resources/db/changelog/db.changelog-master.yaml
touch src/main/resources/db/changelog/changes/001-initial-schema.yaml
After creating these files and directories:
Put the master changelog content into db.changelog-master.yaml
Put the table creation changelog we created earlier into 001-initial-schema.yaml
Now when you start the application, Liquibase should be able to find and execute your changelog files.
Клод добавил db.changelog-master.yaml
файл и даже предложил структуру хранения миграций. Однако теперь возникла новая проблема — ошибка доступа к схеме.
Пытаемся вместе с Клодом решить эту проблему.
Ответ КлодаI've added a changelog to the master file, but when I run the application, a new error occurs: permission denied for schema anthropic_claude
This error means the 'app' role doesn't have sufficient permissions on the schema. Let's fix the permissions by running additional SQL commands as a superuser (postgres):
-- Connect as superuser (postgres) and run these commands:
-- Make sure the role and schema exist
CREATE ROLE app WITH LOGIN PASSWORD 'strongPassword' NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
CREATE SCHEMA IF NOT EXISTS my_app_schema;
-- Grant all necessary permissions
GRANT ALL ON SCHEMA my_app_schema TO app;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA my_app_schema TO app;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA my_app_schema TO app;
-- This is important for Liquibase to be able to create new tables
ALTER DEFAULT PRIVILEGES IN SCHEMA my_app_schema GRANT ALL ON TABLES TO app;
ALTER DEFAULT PRIVILEGES IN SCHEMA my_app_schema GRANT ALL ON SEQUENCES TO app;
-- Make app the owner of the schema (optional, but can help avoid permission issues)
ALTER SCHEMA my_app_schema OWNER TO app;
-- Set the search path
SET search_path TO my_app_schema;
ALTER ROLE app SET search_path TO my_app_schema;
You can run these commands using psql or your preferred PostgreSQL client tool:
psql -U postgres -d your_database_name -f permissions.sql
After running these commands, restart your application and the permission error should be resolved.
Я уже не раз встречался с таким поведением: если LLM сталкивается с несколькими ошибками подряд, он начинает "фантазировать" и предлагать плохие решения. Причём каждое следующее исправление бывает хуже предыдущего. Это типичная проблема всех LLM — они не анализируют историю взаимодействия глобально, а просто пытаются найти ближайшее возможное исправление. Иногда каждое следующее исправление хуже предыдущего. В таких случаях лучше остановиться, пересмотреть ситуацию вручную и задать более конкретные инструкции.
Его предложенное исправление оказалось неверным. Я не стал его мучить и просто добавил фикс вручную:
CREATE SCHEMA my_schema_app AUTHORIZATION app;
После этого приложение запустилось без проблем.
Чем больше ошибок, тем хуже становится выдача. LLM не анализирует всю историю взаимодействий, а просто пытается угадать следующее исправление.
Проверяйте настройки инфраструктуры. Ошибки могут быть незаметными, но критичными.
Первый ПР готов
https://github.com/nzinovev/anthropic-claude/pull/1
Приложение успешно запускается, таблицы созданы, миграции накатываются. Следующий шаг — создать сущности.
Кроме того, я попросил Клода предложить структуру пакетов, чтобы понимать, где хранить код.
Write entities for operation and category tables.
Provide a path to the package where I should create the entities.
Ответ КлодаCreate these entities in the src/main/java/com/yourcompany/domain/entity
package. Here are the entities:
// src/main/java/com/yourcompany/domain/entity/Category.java
package com.yourcompany.domain.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.util.Set;
@Entity
@Table(name = "category", schema = "my_app_schema")
@Getter
@Setter
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@OneToMany(mappedBy = "category")
private Set operations;
}
// src/main/java/com/yourcompany/domain/entity/Operation.java
package com.yourcompany.domain.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Entity
@Table(name = "operation", schema = "my_app_schema")
@Getter
@Setter
public class Operation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "operation_type", nullable = false)
@Enumerated(EnumType.STRING)
private OperationType operationType;
@Column(name = "amount", nullable = false)
private BigDecimal amount;
@Column(name = "description")
private String description;
@Column(name = "operation_date", nullable = false)
private LocalDateTime operationDate;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private Category category;
}
// src/main/java/com/yourcompany/domain/entity/OperationType.java
package com.yourcompany.domain.entity;
public enum OperationType {
DEPOSIT,
WITHDRAW
}
Make sure you have the following dependencies in your pom.xml
:
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-data-jpa
Directory structure should look like this:
src/
└── main/
└── java/
└── com/
└── yourcompany/
└── domain/
└── entity/
├── Category.java
├── Operation.java
└── OperationType.java
Первое, что заметил — Клод начал терять контекст:
В Category
пропало поле description
.
В Operation
неожиданно появилось operationDate
.
Он изменил названия колонок, из-за чего приложение перестало запускаться.
Пришлось вручную исправлять названия колонок. Далее буду добавлять недостающую информацию в контекст, чтобы избежать таких ошибок.
Ещё одна ошибка — не переопределены equals()
и hashCode()
.
Клод использует Set
для Operation
, что без корректного equals()
может привести к дублированию одинаковых объектов с разными ссылками.
Я попросил его переопределить equals()
и hashCode()
, чтобы проверить, не добавит ли он в эти методы объекты OneToMany
/ ManyToOne
и не создаст ли циклическую зависимость (что может привести к OutOfMemory).
Почему важно переопределять equals()
и hashCode()
?
Если этого не делать, объекты сравниваются по ссылке, что может привести к багам.
Lombok-аннотации @EqualsAndHashCode
и @Data
неявно переопределяют эти методы используя все имеющиеся свойства класса (например, могут учитывать связанные сущности, что приведёт к рекурсии).
Лучший вариант — переопределять вручную.
Прошу переопределить equals()
& hashCode()
Override equals and hashCode for Category and Operation entites.
Ответ КлодаHere are the updated entities with equals
and hashCode
methods:
// src/main/java/com/yourcompany/domain/entity/Category.java
package com.yourcompany.domain.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.util.Set;
import java.util.Objects;
@Entity
@Table(name = "category", schema = "my_app_schema")
@Getter
@Setter
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@OneToMany(mappedBy = "category")
private Set operations;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Category category = (Category) o;
if (id == null || category.id == null) return false;
return Objects.equals(id, category.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
// src/main/java/com/yourcompany/domain/entity/Operation.java
package com.yourcompany.domain.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;
@Entity
@Table(name = "operation", schema = "my_app_schema")
@Getter
@Setter
public class Operation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "operation_type", nullable = false)
@Enumerated(EnumType.STRING)
private OperationType operationType;
@Column(name = "amount", nullable = false)
private BigDecimal amount;
@Column(name = "description")
private String description;
@Column(name = "operation_date", nullable = false)
private LocalDateTime operationDate;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private Category category;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Operation operation = (Operation) o;
if (id == null || operation.id == null) return false;
return Objects.equals(id, operation.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
I've implemented equals and hashCode methods using the database identifier (id
) field only. This is a common practice for JPA entities because:
It provides consistent behavior with database identity
It avoids issues with lazy loading
It works well with collections and hibernate's first-level cache
It prevents infinite recursion with bidirectional relationships
Note that the implementation:
Returns false if either object's ID is null
Only compares IDs when both objects are non-null and of the same class
Uses the ID field for hashCode calculation
Клод почти справился, но есть нюансы:
Он избежал циклической зависимости и учёл ленивые загрузки. Однако есть две проблемы:
Использование hashCode = 0
для id = null
, что может снизить производительность HashSet
и HashMap
, если там будет множество объектов с hashCode = 0
.
Проверка классов через getClass()
, т.к. в проекте используется Hibernate, сущности будут использоваться в прокси-объектах, следовательно, данный вызов может некорректно отрабатывать. Лучше использовать instanceof.
Исправленный вариант equals()
и hashCode()
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Category category)) return false; // Лучше, чем getClass() для прокси объектов
return id != null && id.equals(category.id); // Без дополнительной проверки id == null
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : super.hashCode(); // Использование super.hashCode() для уникальности несохранённых объектов
}
Клод также по умолчанию сделал EAGER-связь между Category
и Operation
. На данном этапе это избыточно. Лучше следовать правилу: "Всегда LAZY
, если нет необходимости в EAGER
".
PR с исправлениями
https://github.com/nzinovev/anthropic-claude/pull/2
https://github.com/nzinovev/anthropic-claude/pull/3
Сущности готовы, следующий этап — создание CRUD-операций.
Я рискнул и попросил Клода сразу создать полный цикл для операции CREATE – контроллер → сервис → DAO. Если всё пройдёт гладко, таким же способом реализую и остальные операции (READ, UPDATE, DELETE).
Я не уточнял, какую DTO использовать на вход и выход, чтобы проверить, поймёт ли это Клод самостоятельно или будет работать с сущностями в контроллере.
Чтобы избежать усложнения задачи, я сразу уточнил, что валидация полей не нужна – иначе Клод может потерять фокус и ухудшить качество выдачи.
Write a logic for the Operation entity (Controller - Service - DAO).
In the first iteration, skip validation of all fields.
Что пошло не так? Практически всё.
Я не добавил слово CREATE, а Клод не стал уточнять детали.
В результате он сгенерировал весь CRUD (включая READ, UPDATE, DELETE) – с ошибками.
Исправлять этот код дальше будет неудобно. Легче делать всё шаг за шагом. Именно поэтому я просил Клода не добавлять валидацию – чтобы не перегружать контекст.
Придётся очистить контекст и перезапустить задачу. Покажу, как это сделать без потери истории.
Но сначала разберём ошибки в коде.
I'll create a complete operation management logic layer by layer. Here's the implementation:
// src/main/java/com/yourcompany/dao/OperationRepository.java
package com.yourcompany.dao;
import com.yourcompany.domain.entity.Operation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface OperationRepository extends JpaRepository {
}
// src/main/java/com/yourcompany/service/OperationService.java
package com.yourcompany.service;
import com.yourcompany.domain.entity.Operation;
import java.util.List;
public interface OperationService {
Operation createOperation(Operation operation);
Operation getOperation(Long id);
List getAllOperations();
Operation updateOperation(Long id, Operation operation);
void deleteOperation(Long id);
}
// src/main/java/com/yourcompany/service/impl/OperationServiceImpl.java
package com.yourcompany.service.impl;
import com.yourcompany.dao.OperationRepository;
import com.yourcompany.domain.entity.Operation;
import com.yourcompany.service.OperationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@RequiredArgsConstructor
public class OperationServiceImpl implements OperationService {
private final OperationRepository operationRepository;
@Override
@Transactional
public Operation createOperation(Operation operation) {
return operationRepository.save(operation);
}
@Override
@Transactional(readOnly = true)
public Operation getOperation(Long id) {
return operationRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Operation not found"));
}
@Override
@Transactional(readOnly = true)
public List getAllOperations() {
return operationRepository.findAll();
}
@Override
@Transactional
public Operation updateOperation(Long id, Operation operation) {
Operation existingOperation = getOperation(id);
existingOperation.setOperationType(operation.getOperationType());
existingOperation.setAmount(operation.getAmount());
existingOperation.setDescription(operation.getDescription());
existingOperation.setOperationDate(operation.getOperationDate());
existingOperation.setCategory(operation.getCategory());
return operationRepository.save(existingOperation);
}
@Override
@Transactional
public void deleteOperation(Long id) {
operationRepository.deleteById(id);
}
}
// src/main/java/com/yourcompany/dto/OperationDto.java
package com.yourcompany.dto;
import com.yourcompany.domain.entity.OperationType;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Data
public class OperationDto {
private Long id;
private OperationType operationType;
private BigDecimal amount;
private String description;
private LocalDateTime operationDate;
private Long categoryId;
}
// src/main/java/com/yourcompany/mapper/OperationMapper.java
package com.yourcompany.mapper;
import com.yourcompany.domain.entity.Operation;
import com.yourcompany.dto.OperationDto;
import org.springframework.stereotype.Component;
@Component
public class OperationMapper {
public OperationDto toDto(Operation operation) {
OperationDto dto = new OperationDto();
dto.setId(operation.getId());
dto.setOperationType(operation.getOperationType());
dto.setAmount(operation.getAmount());
dto.setDescription(operation.getDescription());
dto.setOperationDate(operation.getOperationDate());
dto.setCategoryId(operation.getCategory().getId());
return dto;
}
public Operation toEntity(OperationDto dto) {
Operation operation = new Operation();
operation.setOperationType(dto.getOperationType());
operation.setAmount(dto.getAmount());
operation.setDescription(dto.getDescription());
operation.setOperationDate(dto.getOperationDate());
return operation;
}
}
// src/main/java/com/yourcompany/controller/OperationController.java
package com.yourcompany.controller;
import com.yourcompany.domain.entity.Operation;
import com.yourcompany.dto.OperationDto;
import com.yourcompany.mapper.OperationMapper;
import com.yourcompany.service.CategoryService;
import com.yourcompany.service.OperationService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/api/operations")
@RequiredArgsConstructor
public class OperationController {
private final OperationService operationService;
private final CategoryService categoryService;
private final OperationMapper operationMapper;
@PostMapping
public ResponseEntity createOperation(@RequestBody OperationDto operationDto) {
Operation operation = operationMapper.toEntity(operationDto);
operation.setCategory(categoryService.getCategory(operationDto.getCategoryId()));
Operation savedOperation = operationService.createOperation(operation);
return ResponseEntity.ok(operationMapper.toDto(savedOperation));
}
@GetMapping("/{id}")
public ResponseEntity getOperation(@PathVariable Long id) {
Operation operation = operationService.getOperation(id);
return ResponseEntity.ok(operationMapper.toDto(operation));
}
@GetMapping
public ResponseEntity> getAllOperations() {
List operations = operationService.getAllOperations()
.stream()
.map(operationMapper::toDto)
.collect(Collectors.toList());
return ResponseEntity.ok(operations);
}
@PutMapping("/{id}")
public ResponseEntity updateOperation(
@PathVariable Long id,
@RequestBody OperationDto operationDto) {
Operation operation = operationMapper.toEntity(operationDto);
operation.setCategory(categoryService.getCategory(operationDto.getCategoryId()));
Operation updatedOperation = operationService.updateOperation(id, operation);
return ResponseEntity.ok(operationMapper.toDto(updatedOperation));
}
@DeleteMapping("/{id}")
public ResponseEntity deleteOperation(@PathVariable Long id) {
operationService.deleteOperation(id);
return ResponseEntity.noContent().build();
}
}
Note that this implementation assumes you have a CategoryService
with a getCategory(Long id)
method. You'll need to implement that as well.
The directory structure should look like this:
src/
└── main/
└── java/
└── com/
└── yourcompany/
├── controller/
│ └── OperationController.java
├── dao/
│ └── OperationRepository.java
├── dto/
│ └── OperationDto.java
├── mapper/
│ └── OperationMapper.java
└── service/
├── OperationService.java
└── impl/
└── OperationServiceImpl.java
Корректно расставил @Transactional
– разделил readOnly
-транзакции и полноценные.
Добавил DTO – не вернул наружу сущность.
Указал структуру проекта – значит, часть контекста сохранилась.
Предложил дальнейшие шаги.
Сущность "просочилась" в контроллер – маппинг в DTO происходит на уровне контроллера (вне транзакции), что может вызвать LazyInitializationException
.
Сейчас ошибки не будет (мы берём только id
категории), но это ещё хуже – создаётся ложное ощущение, что всё работает правильно.
Не уточнил, какие поля должны быть в DTO.
Из-за этого он вытянул categoryId
наружу, хотя это не критично.
Ошибся с @Transactional
.
Я явно указал DAO-слой, но Клод повесил @Transactional
на сервис.
Это проблема, потому что бизнес-логика (сервис) может вызывать сторонние API – и в таком варианте всё это будет выполняться внутри транзакции, что неэффективно.
Можно ли исправить эти ошибки позднее? Конечно. Но если сделать сразу правильно, это сэкономит много времени в будущем.
Когда LLM изменяет большой кусок кода, он часто редактирует даже те части, которые не нужно трогать.
Это приводит к:
"Захламлению" контекста
Порче логики даже в тех местах, где её не нужно менять
Ухудшению качества последующих ответов
Поэтому проще очистить контекст и задать задачу заново. Я задам новый промпт и поправлю выдачу Клода, чтобы избежать ошибок.
Этот подход показывает, что важно разбивать сложные задачи на этапы.
Очистка контекста – полезный инструмент, если модель начала терять логику.
Дальше попробую создать CREATE-операцию по шагам, чтобы минимизировать ошибки.
Подготавливаю новый промпт с историей взаимодействия:
Скрытый текстYou will be acting as a backend developer. You are have an expertise in the following technologies: Java 21+, Spring boot, Spring JPA, Hibernate, Lombok, Spring Web, REST API, SQL. Your goal is to create a production-ready solution for the user and answer their questions. You should clarify questions to provide the best possible answer. If you have any questions, ask them first without providing a solution. Only after all questions have been clarified, you provide a solution for the user.
You should maintain a friendly and professional tone.
Here are some important rules of conduct:
- If you're not sure how to respond, say: "Sorry, I didn't understand you. Could you please rephrase your question, please?"
- If you don't know the answer to a question, say: "I am sorry, but I don't know that answer. Can you please clarify your question for me?"
Here is the history (between you and user) before the question:
<history>
user: "I have already created an application using Spring Initializr with the following dependencies: Lombok, Spring Web, Spring Data JPA, PostgreSQL Driver, Liquibase Migration. Write a Liquibase migration for the tables.<table1>
Category table with columns: category_id (bigint), category_name (varchar), category_description (varchar)</table1>
<table2>
Operation table with columns: operation_id (bigint), operation_public_id (varchar), operation_name (varchar), operation_amount(bigdecimal), operation_type (enum), category_id (FK to category table)
</table2>
Suggest how to store the operation_type. In the code, the operation type will be an Enum with the values (DEPOSIT, WITHDRAW)."
You: "Let me analyze your requirements:
Database Structure:
Two tables: Category and Operation
Category is the parent table with a primary key referenced by Operation
Operation has an enum field operation_type
Questions for clarification:
Should category_id in both tables be auto-incrementing?
What should be the maximum lengths for varchar fields (category_name, category_description, operation_public_id, operation_name)?
Should any of these fields be nullable?
For operation_amount, how many digits before and after decimal point do you need?
Should operation_public_id be unique?
Before I provide the Liquibase migration script, please answer these questions so I can create the most appropriate solution for your needs.
Regarding the operation_type storage, I suggest two approaches:
As a VARCHAR field with a check constraint limiting values to 'DEPOSIT' and 'WITHDRAW'
As a custom ENUM type in PostgreSQL
The first approach is generally more flexible for future modifications and better portable across different databases. Would you like me to proceed with this approach, or would you prefer the PostgreSQL ENUM type?
Please provide your preferences so I can create a complete migration script."
user: "1. Correct
Questions for clarification:
Yes, category_id and operation_id should be auto-incremented
For category_name, operation_public_id and operation_name - varchar(255), and for category_description (varchar 1000)
Nullable fields: category_description
20 digits before decimal point and 2 after
Yes
Regarding the operation_type, let's take the first approach."
you: "Thank you for the clarification. I'll create a Liquibase migration script that meets all these requirements.
databaseChangeLog:
- changeSet:
id: 001_create_category_table
author: developer
changes:
- createTable:
tableName: category
columns:
- column:
name: category_id
type: bigint
constraints:
primaryKey: true
nullable: false
- column:
name: category_name
type: varchar(255)
constraints:
nullable: false
- column:
name: category_description
type: varchar(1000)
constraints:
nullable: true
- addAutoIncrement:
tableName: category
columnName: category_id
columnDataType: bigint
startWith: 1
incrementBy: 1
- changeSet:
id: 002_create_operation_table
author: developer
changes:
- createTable:
tableName: operation
columns:
- column:
name: operation_id
type: bigint
constraints:
primaryKey: true
nullable: false
- column:
name: operation_public_id
type: varchar(255)
constraints:
nullable: false
unique: true
- column:
name: operation_name
type: varchar(255)
constraints:
nullable: false
- column:
name: operation_amount
type: decimal(22,2)
constraints:
nullable: false
- column:
name: operation_type
type: varchar(255)
constraints:
nullable: false
- column:
name: category_id
type: bigint
constraints:
nullable: false
foreignKeyName: fk_operation_category
references: category(category_id)
- addAutoIncrement:
tableName: operation
columnName: operation_id
columnDataType: bigint
startWith: 1
incrementBy: 1
- changeSet:
id: 003_add_operation_type_constraint
author: developer
changes:
- sql:
sql: ALTER TABLE operation ADD CONSTRAINT check_operation_type CHECK (operation_type IN ('DEPOSIT', 'WITHDRAW'));
Would you like me to explain the different parts of this migration script?"
user: "Write the configuration to connect the application to the PostgreSQL database."
you: "I'll help you configure the database connection. Let me write the configuration for both development and production environments using YAML format.
# src/main/resources/application.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/your_database_name
username: your_username
password: your_password
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: validate # validates the schema, makes no changes to the database
show-sql: false # set to true if you want to see SQL queries in logs
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true # formats SQL queries in logs if show-sql is true
liquibase:
change-log: classpath:db/changelog/db.changelog-master.yaml
enabled: true # enables Liquibase auto-deployment of changes
# Connection pool properties (using HikariCP - Spring Boot default)
hikari:
minimum-idle: 5
maximum-pool-size: 20
idle-timeout: 300000 # 5 minutes
pool-name: HikariPool
max-lifetime: 1200000 # 20 minutes
connection-timeout: 20000 # 20 seconds
For different environments, you can create separate configuration files:
# src/main/resources/application-dev.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/dev_database
username: dev_user
password: dev_password
jpa:
show-sql: true
# src/main/resources/application-prod.yml
spring:
datasource:
url: jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
jpa:
show-sql: false
To use these profiles, you can:
Add in your main application.yml:
spring:
profiles:
active: dev # or prod
Or set it via command line:
java -jar your-app.jar --spring.profiles.active=dev
Or set it via environment variable:
export SPRING_PROFILES_ACTIVE=dev
For production environment, it's recommended to:
Use environment variables for sensitive data
Never commit real credentials to version control
Use strong passwords
Consider using connection encryption (SSL)
Would you like me to explain any part of this configuration in more detail?"
user: "Write entities for operation and category tables. Provide a path to the package where I should create the entities."
you: "Create these entities in the src/main/java/com/yourcompany/domain/entity package. Here are the entities:
// src/main/java/com/yourcompany/domain/entity/Category.java
package com.yourcompany.domain.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.util.Set;
@Entity
@Table(name = "category", schema = "my_app_schema")
@Getter
@Setter
public class Category {
@Id
@Column(name = "category_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "category_name", nullable = false)
private String name;
@Column(name = "category_description")
private String description;
@OneToMany(mappedBy = "category", fetch = FetchType.LAZY)
private Set operations;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Category category)) return false;
return id != null && id.equals(category.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : super.hashCode();
}
}
// src/main/java/com/yourcompany/domain/entity/Operation.java
package com.yourcompany.domain.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Entity
@Table(name = "operation", schema = "my_app_schema")
@Getter
@Setter
public class Operation {
@Id
@Column(name = "operation_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "operation_public_id", nullable = false, unique = true)
private String publicId;
@Column(name = "operation_name", nullable = false)
private String name;
@Column(name = "operation_type", nullable = false)
@Enumerated(EnumType.STRING)
private OperationType type;
@Column(name = "operation_amount", nullable = false)
private BigDecimal amount;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private Category category;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Operation operation)) return false;
return id != null && id.equals(operation.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : super.hashCode();
}
}
// src/main/java/com/yourcompany/domain/entity/OperationType.java
package com.yourcompany.domain.entity;
public enum OperationType {
DEPOSIT,
WITHDRAW
}
Make sure you have the following dependencies in your pom.xml:
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
Directory structure should look like this:
src/
└── main/
└── java/
└── com/
└── yourcompany/
└── domain/
└── entity/
├── Category.java
├── Operation.java
└── OperationType.java
Would you like me to explain any part of these entities or their relationships?"
</history>
Here is the user question:
<question>
Write a CREATE logic for the Operation entity (Controller - Service - DAO). In the first iteration, skip validation of all fields.</question>
Think about the answer first before responding. Think step-by-step.
Пришлось два раза переписывать промпт — из-за ошибок в формулировке.
В первый раз Клод уточнял детали, и код получился чуть качественнее, но здесь я разберу второй вариант, так как именно он был отправлен в работу.
Сброс контекста и обновление промпта помогают ИИ вспомнить начальные установки.
Это один из способов повысить точность ответа, но у него есть минус — такой подход требует больше времени, так как приходится собирать историю общения с моделью (тег <history>
в промпте).
Я советую использовать теги <history></history>
только в двух случаях:
ИИ зашёл в тупик и выдаёт некорректные решения.
Были ошибки в промпте и хочется избавиться от ошибок.
Преимущество этого метода — возможность редактировать историю общения, а также восстановление изначальных требований по взаимодействию модели с пользователем, что в конечном счёте улучшает итоговый результат.
Ответ КлодаThank you for the clarification. I'll create a complete solution for the operation creation logic.
// src/main/java/com/yourcompany/dto/request/CreateOperationRequest.java
package com.yourcompany.dto.request;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class CreateOperationRequest {
private String operationName;
private String operationType;
private BigDecimal amount;
private String categoryName;
}
// src/main/java/com/yourcompany/dto/response/OperationDto.java
package com.yourcompany.dto.response;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class OperationDto {
private String operationPublicId;
private String operationName;
private String operationType;
private BigDecimal amount;
private String categoryName;
}
// src/main/java/com/yourcompany/mapper/OperationMapper.java
package com.yourcompany.mapper;
import com.yourcompany.domain.entity.Operation;
import com.yourcompany.dto.response.OperationDto;
import org.springframework.stereotype.Component;
@Component
public class OperationMapper {
public OperationDto toDto(Operation operation) {
OperationDto dto = new OperationDto();
dto.setOperationPublicId(operation.getOperationPublicId());
dto.setOperationName(operation.getOperationName());
dto.setOperationType(operation.getOperationType().name());
dto.setAmount(operation.getAmount());
dto.setCategoryName(operation.getCategory().getName());
return dto;
}
}
// src/main/java/com/yourcompany/repository/OperationRepository.java
package com.yourcompany.repository;
import com.yourcompany.domain.entity.Operation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface OperationRepository extends JpaRepository {
}
// src/main/java/com/yourcompany/repository/CategoryRepository.java
package com.yourcompany.repository;
import com.yourcompany.domain.entity.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface CategoryRepository extends JpaRepository {
Optional findByName(String name);
}
// src/main/java/com/yourcompany/service/OperationService.java
package com.yourcompany.service;
import com.yourcompany.domain.entity.Category;
import com.yourcompany.domain.entity.Operation;
import com.yourcompany.domain.entity.OperationType;
import com.yourcompany.dto.request.CreateOperationRequest;
import com.yourcompany.dto.response.OperationDto;
import com.yourcompany.mapper.OperationMapper;
import com.yourcompany.repository.CategoryRepository;
import com.yourcompany.repository.OperationRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
import java.util.UUID;
@Service
@RequiredArgsConstructor
public class OperationService {
private final OperationRepository operationRepository;
private final CategoryRepository categoryRepository;
private final OperationMapper operationMapper;
@Transactional
public OperationDto createOperation(CreateOperationRequest request) {
Category category = categoryRepository.findByName(request.getCategoryName())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"Category not found: " + request.getCategoryName()));
Operation operation = new Operation();
operation.setOperationPublicId(UUID.randomUUID().toString());
operation.setOperationName(request.getOperationName());
operation.setOperationType(OperationType.valueOf(request.getOperationType()));
operation.setAmount(request.getAmount());
operation.setCategory(category);
Operation savedOperation = operationRepository.save(operation);
return operationMapper.toDto(savedOperation);
}
}
// src/main/java/com/yourcompany/controller/OperationController.java
package com.yourcompany.controller;
import com.yourcompany.dto.request.CreateOperationRequest;
import com.yourcompany.dto.response.OperationDto;
import com.yourcompany.service.OperationService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/operations")
@RequiredArgsConstructor
public class OperationController {
private final OperationService operationService;
@PostMapping
public ResponseEntity createOperation(@RequestBody CreateOperationRequest request) {
OperationDto response = operationService.createOperation(request);
return ResponseEntity.ok(response);
}
}
Ограничение задачи позволило Клоду улучшить качество кода:
Теперь сервис не возвращает сущность в контроллер.
Код стал чище, а выдача качественнее.
Добавлены простейшие бизнес-обработки.
Но не обошлось без недостатков - Нет @JsonProperty в DTO. Наличие этой аннотации делает безопасным изменения полей DTO в коде без влияния на API-контракт.
Отправляем запрос:
{
"name": "Заправка",
"amount": 125.23,
"type": "WITHDRAW",
"categoryId": 1
}
Получаем ответ:
{
"publicId":"31cf0666-38b6-4aa3-9c6d-2547fe15e237",
"name":"Заправка",
"amount":125.23,
"type":"WITHDRAW",
"categoryId":1
}
Я попробовал два подхода:
Создание всех CRUD-операций сразу.
Создание каждой операции по отдельности.
Второй вариант оказался качественнее, поэтому по нему будут реализованы READ, UPDATE, DELETE.
PR на создание операции
https://github.com/nzinovev/anthropic-claude/pull/5/files
Write an UPDATE logic for the Operation entity (Controller - Service - DAO).
In the first iteration, skip validation of all fields.
Ответ КлодаLet me design the UPDATE logic step by step:
Components needed:
Controller endpoint for PUT/PATCH request
Service method for update logic
DTO for update request
Mapper update method
Flow:
Find operation by publicId (more secure than using internal ID)
Update only provided fields
Return updated operation
Here's the implementation:
// src/main/java/com/yourcompany/dto/OperationUpdateRequest.java
package com.yourcompany.dto;
import com.yourcompany.domain.entity.OperationType;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class OperationUpdateRequest {
private String name;
private BigDecimal amount;
private OperationType type;
private Long categoryId;
}
// src/main/java/com/yourcompany/mapper/OperationMapper.java
// Add this method to the existing OperationMapper class
package com.yourcompany.mapper;
public class OperationMapper {
// ... existing methods ...
public void updateEntityFromDto(OperationUpdateRequest request, Operation operation) {
if (request.getName() != null) {
operation.setName(request.getName());
}
if (request.getAmount() != null) {
operation.setAmount(request.getAmount());
}
if (request.getType() != null) {
operation.setType(request.getType());
}
}
}
// src/main/java/com/yourcompany/repository/OperationRepository.java
// Add this method to the existing OperationRepository interface
package com.yourcompany.repository;
public interface OperationRepository extends JpaRepository {
Optional findByPublicId(String publicId);
}
// src/main/java/com/yourcompany/service/OperationService.java
// Add these methods to the existing OperationService class
package com.yourcompany.service;
@Service
@RequiredArgsConstructor
public class OperationService {
// ... existing methods ...
@Transactional
public OperationResponse updateOperation(String publicId, OperationUpdateRequest request) {
Operation operation = operationRepository.findByPublicId(publicId)
.orElseThrow(() -> new RuntimeException("Operation not found"));
operationMapper.updateEntityFromDto(request, operation);
if (request.getCategoryId() != null) {
Category category = categoryRepository.getReferenceById(request.getCategoryId());
operation.setCategory(category);
}
Operation updatedOperation = operationRepository.save(operation);
return operationMapper.toDto(updatedOperation);
}
}
// src/main/java/com/yourcompany/controller/OperationController.java
// Add this method to the existing OperationController class
package com.yourcompany.controller;
@RestController
@RequestMapping("/api/v1/operations")
@RequiredArgsConstructor
public class OperationController {
// ... existing methods ...
@PutMapping("/{publicId}")
public OperationResponse updateOperation(
@PathVariable String publicId,
@RequestBody OperationUpdateRequest request) {
return operationService.updateOperation(publicId, request);
}
}
// src/main/java/com/yourcompany/exception/OperationNotFoundException.java
package com.yourcompany.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class OperationNotFoundException extends RuntimeException {
public OperationNotFoundException(String message) {
super(message);
}
}
Now you can update an operation by making a PUT request to /api/v1/operations/{publicId} with a JSON body containing the fields you want to update. For example:
{
"name": "Updated Operation Name",
"amount": 150.00,
"type": "DEPOSIT",
"categoryId": 2
}
Клод добавил какую-то странную конструкцию:
operationMapper.updateEntityFromDto(operation, dto);
Звучит логично, но есть несколько проблем:
Вынес в маппер логику, которая к нему не относится.
Разделил её по разным слоям: три параметра проставляются в маппере, а один — в сервисе.
Это плохой дизайн, потому что маппер должен просто преобразовывать DTO в сущность и обратно, а не вносить изменения в объект.
Клод добавил кастомное исключение:
public class OperationNotFoundException extends RuntimeException { ... }
На первый взгляд, всё правильно, но есть один нюанс — он его нигде не использует!
Там, где должно было бросаться OperationNotFoundException
, Клод выбрасывает обычный RuntimeException
. В итоге, хорошая идея, но не реализованная до конца.
Далее будут появятся другие исключения в том же стиле (например CategoryNotFoundException
). В итоге каждое исключение будет уникальным — это усложнит поддержку. Лучше сделать одно NotFoundException
, а при необходимости — наследовать от него:
public class NotFoundException extends RuntimeException {
public NotFoundException(String message) {
super(message);
}
}
А затем:
public class OperationNotFoundException extends NotFoundException {
public OperationNotFoundException() {
super("Operation not found");
}
}
Такой подход даёт два преимущества:
Единый ExceptionHandler
– можно обрабатывать NotFoundException
, а не кучу отдельных классов.
Чище код – если появится новый тип NotFound
, его не нужно будет добавлять в обработчик.
Несмотря на эти проблемы, Клод справился с задачей. Код рабочий, даже был предложен тестовый JSON для проверки.
Теперь переходим к поиску операций (READ).
PR на обновление операции
https://github.com/nzinovev/anthropic-claude/pull/6
Попросил Клода создать два эндпоинта:
Получение всех операций с пагинацией.
Поиск операции по publicId.
Write a FIND logic for the Operation entity (Controller - Service - DAO).
In the first iteration, skip validation of all fields.
You need to add two endpoints, the first one that finds all operations with pagination,
and the second one that finds an operation by publicId.
Ответ КлодаLet me design the FIND logic step by step:
Components needed:
Controller endpoints for GET requests (findAll with pagination and findByPublicId)
Service methods for both operations
Repository method for finding by publicId (already exists)
DTO for pagination criteria (optional parameters like page, size, sort)
Flow:
Accept pagination parameters
Convert Page to Page
Return paginated response or single operation
Here's the implementation:
// src/main/java/com/yourcompany/dto/PageResponse.java
package com.yourcompany.dto;
import lombok.Data;
import org.springframework.data.domain.Page;
import java.util.List;
@Data
public class PageResponse {
private List content;
private int pageNumber;
private int pageSize;
private long totalElements;
private int totalPages;
private boolean last;
public static PageResponse from(Page page) {
PageResponse response = new PageResponse<>();
response.setContent(page.getContent());
response.setPageNumber(page.getNumber());
response.setPageSize(page.getSize());
response.setTotalElements(page.getTotalElements());
response.setTotalPages(page.getTotalPages());
response.setLast(page.isLast());
return response;
}
}
// src/main/java/com/yourcompany/service/OperationService.java
// Add these methods to the existing OperationService class
package com.yourcompany.service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@Service
@RequiredArgsConstructor
public class OperationService {
// ... existing methods ...
@Transactional(readOnly = true)
public PageResponse findAll(Pageable pageable) {
Page operationPage = operationRepository.findAll(pageable);
Page responsePage = operationPage.map(operationMapper::toDto);
return PageResponse.from(responsePage);
}
@Transactional(readOnly = true)
public OperationResponse findByPublicId(String publicId) {
Operation operation = operationRepository.findByPublicId(publicId)
.orElseThrow(() -> new OperationNotFoundException(
String.format("Operation with publicId %s not found", publicId)));
return operationMapper.toDto(operation);
}
}
// src/main/java/com/yourcompany/controller/OperationController.java
// Add these methods to the existing OperationController class
package com.yourcompany.controller;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
@RestController
@RequestMapping("/api/v1/operations")
@RequiredArgsConstructor
public class OperationController {
// ... existing methods ...
@GetMapping
public PageResponse findAll(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size,
@RequestParam(defaultValue = "operation_id") String sortBy,
@RequestParam(defaultValue = "DESC") Sort.Direction direction) {
PageRequest pageRequest = PageRequest.of(
page,
size,
Sort.by(direction, sortBy)
);
return operationService.findAll(pageRequest);
}
@GetMapping("/{publicId}")
public OperationResponse findByPublicId(@PathVariable String publicId) {
return operationService.findByPublicId(publicId);
}
}
Now you can:
Get a paginated list of operations:
GET /api/v1/operations?page=0&size=20&sortBy=operation_id&direction=DESC
Response example:
{
"content": [
{
"publicId": "550e8400-e29b-41d4-a716-446655440000",
"name": "Operation 1",
"amount": 100.00,
"type": "DEPOSIT",
"categoryId": 1
},
// ... more operations
],
"pageNumber": 0,
"pageSize": 20,
"totalElements": 100,
"totalPages": 5,
"last": false
}
Get a single operation by publicId:
GET /api/v1/operations/550e8400-e29b-41d4-a716-446655440000
Response example:
{
"publicId": "550e8400-e29b-41d4-a716-446655440000",
"name": "Operation 1",
"amount": 100.00,
"type": "DEPOSIT",
"categoryId": 1
}
Клод использовал пагинацию Spring JPA, что логично, так как мы работаем с Hibernate.
Код получился почти идеальным:
PageResponse
сделан дженериком – можно переиспользовать для любых ответов.
Контекст сохраняется – Клод не дублирует уже написанный код.
Проблема с sortBy
— Клод ссылается на колонку в БД, а не на поле сущности. В таком виде сортировка не будет работать, т.к. JPA для сортировки использует именно название полей сущности.
Spring Data JPA использует рефлексию для работы с сущностями. Когда мы передаем параметр сортировки sortBy
, Spring Data JPA пытается найти соответствующее поле по имени внутри класса сущности. JPA ищет свойство (field) внутри Java-класса Operation
, а не колонку в базе данных.
Исправляем и переходим к DELETE.
PR на поиск операций
https://github.com/nzinovev/anthropic-claude/pull/7
Финальная часть CRUD-операций – удаление. Прошу Клода написать эндпоинт, который удаляет операцию по publicId.
Write a DELETE logic for the Operation entity (Controller - Service - DAO).
In the first iteration, skip validation of all fields.
Ответ КлодаLet me design the DELETE logic step by step:
Components needed:
Controller endpoint for DELETE request
Service method for delete operation
Repository method (will use existing findByPublicId)
Flow:
Find operation by publicId
Delete operation if exists
Return appropriate response status
Here's the implementation:
// src/main/java/com/yourcompany/service/OperationService.java
// Add this method to the existing OperationService class
package com.yourcompany.service;
@Service
@RequiredArgsConstructor
public class OperationService {
// ... existing methods ...
@Transactional
public void deleteOperation(String publicId) {
Operation operation = operationRepository.findByPublicId(publicId)
.orElseThrow(() -> new OperationNotFoundException(
String.format("Operation with publicId %s not found", publicId)));
operationRepository.delete(operation);
}
}
// src/main/java/com/yourcompany/controller/OperationController.java
// Add this method to the existing OperationController class
package com.yourcompany.controller;
@RestController
@RequestMapping("/api/v1/operations")
@RequiredArgsConstructor
public class OperationController {
// ... existing methods ...
@DeleteMapping("/{publicId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteOperation(@PathVariable String publicId) {
operationService.deleteOperation(publicId);
}
}
Now you can delete an operation by making a DELETE request to:
DELETE /api/v1/operations/{publicId}
Клод справился без проблем – здесь всё очевидно, комментировать особо нечего.
PR на удаление операции
https://github.com/nzinovev/anthropic-claude/pull/8
CRUD для operation
готов, но пока он довольно примитивен. Кроме того, у нас вообще нет тестов – а это важная часть качественной разработки. Чтобы не растягивать статью, я не буду покрывать тестами и валидацией все эндпоинты.
Я выберу один – CREATE Operation
– и попрошу Клода добавить:
Валидацию запроса
Unit-тесты для сервисного слоя
MVC-тест для контроллера
Погнали!
Напоминаю Клоду, как выглядит логика, связанная с созданием операции и прошу добавить валидацию для CREATE-операции.
Запрос КлодуThis is how the "create operation" is implemented at the moment.
<example>
@Data
public class OperationCreateRequest {
private String name;
private BigDecimal amount;
private OperationType type;
private Long categoryId;
}
@RestController
@RequestMapping("/api/v1/operations")
@RequiredArgsConstructor
public class OperationController {
private final OperationService operationService;
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public OperationResponse createOperation(@RequestBody OperationCreateRequest request) {
return operationService.createOperation(request);
}
}
@Service
@RequiredArgsConstructor
public class OperationService {
private final OperationRepository operationRepository;
private final CategoryRepository categoryRepository;
private final OperationMapper operationMapper;
@Transactional
public OperationResponse createOperation(OperationCreateRequest request) {
Category category = categoryRepository.getReferenceById(request.getCategoryId());
Operation operation = operationMapper.toEntity(request);
operation.setCategory(category);
Operation savedOperation = operationRepository.save(operation);
return operationMapper.toDto(savedOperation);
}
}
@Entity
@Table(name = "operation", schema = "anthropic_claude")
@Getter
@Setter
public class Operation {
@Id
@Column(name = "operation_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "operation_public_id", nullable = false, unique = true)
private String publicId;
@Column(name = "operation_name", nullable = false)
private String name;
@Column(name = "operation_type", nullable = false)
@Enumerated(EnumType.STRING)
private OperationType type;
@Column(name = "operation_amount", nullable = false)
private BigDecimal amount;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private Category category;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Operation operation)) return false;
return id != null && id.equals(operation.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : super.hashCode();
}
}
</example>
Add validation for "create operation" logic.
I'll add validation for the create operation logic. Let's implement it step by step:
Field validations needed:
name: not blank, max length 255
amount: not null, positive
type: not null, valid enum value
categoryId: not null, must exist in database
business rule: category must exist
Changes needed:
Add validation annotations to DTO
Add custom exception classes
Add validation in service layer
Add global exception handler
Here's the implementation:
// src/main/java/com/yourcompany/exception/CategoryNotFoundException.java
package com.yourcompany.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class CategoryNotFoundException extends RuntimeException {
public CategoryNotFoundException(String message) {
super(message);
}
}
// src/main/java/com/yourcompany/exception/ValidationErrorResponse.java
package com.yourcompany.exception;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class ValidationErrorResponse {
private List errors = new ArrayList<>();
@Data
public static class ValidationError {
private final String field;
private final String message;
}
}
// src/main/java/com/yourcompany/exception/GlobalExceptionHandler.java
package com.yourcompany.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ValidationErrorResponse handleValidationException(MethodArgumentNotValidException ex) {
ValidationErrorResponse response = new ValidationErrorResponse();
ex.getBindingResult().getFieldErrors().forEach(error ->
response.getErrors().add(new ValidationErrorResponse.ValidationError(
error.getField(),
error.getDefaultMessage()
))
);
return response;
}
@ExceptionHandler(CategoryNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ValidationErrorResponse handleCategoryNotFoundException(CategoryNotFoundException ex) {
ValidationErrorResponse response = new ValidationErrorResponse();
response.getErrors().add(new ValidationErrorResponse.ValidationError(
"categoryId",
ex.getMessage()
));
return response;
}
}
// src/main/java/com/yourcompany/dto/OperationCreateRequest.java
package com.yourcompany.dto;
import com.yourcompany.domain.entity.OperationType;
import jakarta.validation.constraints.*;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class OperationCreateRequest {
@NotBlank(message = "Operation name is required")
@Size(max = 255, message = "Operation name must not exceed 255 characters")
private String name;
@NotNull(message = "Amount is required")
@Positive(message = "Amount must be positive")
@Digits(integer = 20, fraction = 2, message = "Amount must have at most 20 digits and 2 decimal places")
private BigDecimal amount;
@NotNull(message = "Operation type is required")
private OperationType type;
@NotNull(message = "Category ID is required")
@Positive(message = "Category ID must be positive")
private Long categoryId;
}
// src/main/java/com/yourcompany/service/OperationService.java
@Service
@RequiredArgsConstructor
public class OperationService {
private final OperationRepository operationRepository;
private final CategoryRepository categoryRepository;
private final OperationMapper operationMapper;
@Transactional
public OperationResponse createOperation(OperationCreateRequest request) {
// Check if category exists
Category category = categoryRepository.findById(request.getCategoryId())
.orElseThrow(() -> new CategoryNotFoundException(
String.format("Category with id %d not found", request.getCategoryId())));
Operation operation = operationMapper.toEntity(request);
operation.setCategory(category);
Operation savedOperation = operationRepository.save(operation);
return operationMapper.toDto(savedOperation);
}
}
// src/main/java/com/yourcompany/controller/OperationController.java
@RestController
@RequestMapping("/api/v1/operations")
@RequiredArgsConstructor
public class OperationController {
private final OperationService operationService;
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public OperationResponse createOperation(@Valid @RequestBody OperationCreateRequest request) {
return operationService.createOperation(request);
}
}
Now when creating an operation:
If validation fails, you'll get a 400 Bad Request with details about what went wrong:
{
"errors": [
{
"field": "name",
"message": "Operation name is required"
},
{
"field": "amount",
"message": "Amount must be positive"
}
]
}
If category is not found, you'll get a 404 Not Found:
{
"errors": [
{
"field": "categoryId",
"message": "Category with id 123 not found"
}
]
}
Клод реализовал грамотную обработку ошибок:
Все ошибки возвращаются сразу – пользователю не нужно отправлять несколько запросов.
Добавлен ExceptionHandler
– обработка ошибок централизована.
Но валидация не работает.
Клод не добавил нужные зависимости:
<!-- Чтобы заработала аннотация Valid и Spring начал валидацию -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- Для аннотаций NotBlank, NotNull, etc. -->
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.1.0</version>
</dependency>
Если необходимость jakarta.validation
ещё можно догадаться по импорту аннотаций в DTO, то spring-boot-starter-validation
менее очевиден.
А я ведь имитирую разработчика с минимальным опытом, и без документации мне сложно понять, почему валидация не работает.
Как и в CRUD-логике, Клод добавил специфичное исключение:
public class CategoryNotFoundException extends RuntimeException { ... }
Такой подход требует:
Создавать отдельное исключение для каждой сущности.
Добавлять его обработку в ExceptionHandler
.
Лучше использовать единое NotFoundException
и наследоваться от него (это уже обсуждалось ранее).
Кроме того, добавлена лишняя аннотация:
@ResponseStatus(HttpStatus.NOT_FOUND)
Это избыточно, потому что исключение уже обрабатывается глобальным ExceptionHandler
.
Попросил Клода исправить ошибки:
I've added annotations to OperationCreateRequest.
However, when I send a request with incorrect data,
the application does not respond with a validation error.
The app ignores all annotations relating to validation and passes the request on.
Ответ КлодаThe issue is likely because the validation dependencies and configuration are missing. Let's fix this:
First, add the validation dependency to your pom.xml
:
org.springframework.boot
spring-boot-starter-validation
Make sure your application configuration enables validation. Create or update the configuration class:
// src/main/java/com/yourcompany/config/WebConfig.java
package com.yourcompany.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
// Additional web configuration can be added here if needed
}
Double-check that your controller has the @Valid
annotation on the request parameter:
@RestController
@RequestMapping("/api/v1/operations")
@RequiredArgsConstructor
public class OperationController {
private final OperationService operationService;
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public OperationResponse createOperation(@Valid @RequestBody OperationCreateRequest request) {
return operationService.createOperation(request);
}
}
Verify that your DTO has all the necessary validation annotations:
@Data
public class OperationCreateRequest {
@NotBlank(message = "Operation name is required")
@Size(max = 255, message = "Operation name must not exceed 255 characters")
private String name;
@NotNull(message = "Amount is required")
@Positive(message = "Amount must be positive")
@Digits(integer = 20, fraction = 2, message = "Amount must have at most 20 digits and 2 decimal places")
private BigDecimal amount;
@NotNull(message = "Operation type is required")
private OperationType type;
@NotNull(message = "Category ID is required")
@Positive(message = "Category ID must be positive")
private Long categoryId;
}
Make sure you have the exception handler properly configured:
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ValidationErrorResponse handleValidationException(MethodArgumentNotValidException ex) {
ValidationErrorResponse response = new ValidationErrorResponse();
ex.getBindingResult().getFieldErrors().forEach(error ->
response.getErrors().add(new ValidationErrorResponse.ValidationError(
error.getField(),
error.getDefaultMessage()
))
);
return response;
}
// Add logging to help with debugging
public GlobalExceptionHandler() {
log.info("GlobalExceptionHandler initialized");
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ValidationErrorResponse handleAllUncaughtException(Exception ex) {
log.error("Uncaught exception", ex);
ValidationErrorResponse response = new ValidationErrorResponse();
response.getErrors().add(new ValidationErrorResponse.ValidationError(
"global",
"An unexpected error occurred"
));
return response;
}
}
Test the validation with an invalid request:
curl -X POST http://localhost:8080/api/v1/operations \
-H "Content-Type: application/json" \
-d '{
"name": "",
"amount": -100,
"type": null,
"categoryId": null
}'
You should receive a response like:
{
"errors": [
{
"field": "name",
"message": "Operation name is required"
},
{
"field": "amount",
"message": "Amount must be positive"
},
{
"field": "type",
"message": "Operation type is required"
},
{
"field": "categoryId",
"message": "Category ID is required"
}
]
}
If you're still not seeing validation errors, you can add debug logging to verify the validation is being triggered:
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public OperationResponse createOperation(@Valid @RequestBody OperationCreateRequest request) {
log.debug("Received operation create request: {}", request);
return operationService.createOperation(request);
}
Also, you can enable debug logging for Spring validation in application.yml
:
logging:
level:
org.springframework.web: DEBUG
org.springframework.validation: DEBUG
com.yourcompany: DEBUG
Would you like me to help you debug this further or explain any part of the validation setup in more detail?
Ответ дан, но слишком избыточен. Модель начала добавлять ненужный код, в том числе:
// src/main/java/com/yourcompany/config/WebConfig.java
package com.yourcompany.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
// Additional web configuration can be added here if needed
}
Этот класс вообще не нужен – валидация работает без него. Это конфигурация для MVC приложения, а у нас REST API.
Проблема знакомая – при сложных запросах LLM начинает генерировать лишний код.
Можно было очистить контекст и пересоздать промпт, но я решил продолжить с текущим вариантом. Валидация готова, переходим к тестам.
PR с валидацией
https://github.com/nzinovev/anthropic-claude/pull/9
Попросил Клода написать позитивные и негативные тесты для OperationService
Запрос к КлодуThis is a reminder of what code is contained in the OperationService class:
@Service
@RequiredArgsConstructor
public class OperationService {
private final OperationRepository operationRepository;
private final CategoryRepository categoryRepository;
private final OperationMapper operationMapper;
@Transactional
public OperationResponse createOperation(OperationCreateRequest request) {
final var category = categoryRepository.findById(request.getCategoryId())
.orElseThrow(() -> new CategoryNotFoundException(
String.format("Category with id %d not found", request.getCategoryId())));
final var operation = operationMapper.toEntity(request);
operation.setCategory(category);
final var savedOperation = operationRepository.save(operation);
return operationMapper.toDto(savedOperation);
}
@Transactional
public OperationResponse updateOperation(String publicId, OperationUpdateRequest request) {
return operationRepository.findByPublicId(publicId)
.map(operation -> updateOperation(operation, request))
.orElseThrow(() -> new RuntimeException("Operation not found"));
}
@Transactional(readOnly = true)
public PageResponse findAll(Pageable pageable) {
final var operationPage = operationRepository.findAll(pageable);
final var responsePage = operationPage.map(operationMapper::toDto);
return PageResponse.from(responsePage);
}
@Transactional(readOnly = true)
public OperationResponse findByPublicId(String publicId) {
final var operation = operationRepository.findByPublicId(publicId)
.orElseThrow(() -> new RuntimeException("Operation not found"));
return operationMapper.toDto(operation);
}
@Transactional
public void deleteOperation(String publicId) {
final var operation = operationRepository.findByPublicId(publicId)
.orElseThrow(() -> new RuntimeException(
String.format("Operation with publicId %s not found", publicId)));
operationRepository.delete(operation);
}
private OperationResponse updateOperation(Operation operation, OperationUpdateRequest request) {
if (request.getName() != null) {
operation.setName(request.getName());
}
if (request.getAmount() != null) {
operation.setAmount(request.getAmount());
}
if (request.getType() != null) {
operation.setType(request.getType());
}
if (request.getCategoryId() != null) {
final var category = categoryRepository.getReferenceById(request.getCategoryId());
operation.setCategory(category);
}
final var updatedOperation = operationRepository.save(operation);
return operationMapper.toDto(updatedOperation);
}
}
Write unit-tests for OperationService. You should write positive and negative tests where it's needed (for example: check that an exception will be thrown)
I'll write comprehensive unit tests for the OperationService class. We'll use JUnit 5 and Mockito for testing.
// src/test/java/com/yourcompany/service/OperationServiceTest.java
package com.yourcompany.service;
import com.yourcompany.domain.entity.Category;
import com.yourcompany.domain.entity.Operation;
import com.yourcompany.domain.entity.OperationType;
import com.yourcompany.dto.OperationCreateRequest;
import com.yourcompany.dto.OperationResponse;
import com.yourcompany.dto.OperationUpdateRequest;
import com.yourcompany.dto.PageResponse;
import com.yourcompany.exception.CategoryNotFoundException;
import com.yourcompany.mapper.OperationMapper;
import com.yourcompany.repository.CategoryRepository;
import com.yourcompany.repository.OperationRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class OperationServiceTest {
@Mock
private OperationRepository operationRepository;
@Mock
private CategoryRepository categoryRepository;
@Mock
private OperationMapper operationMapper;
@InjectMocks
private OperationService operationService;
private Operation operation;
private Category category;
private OperationResponse operationResponse;
private OperationCreateRequest createRequest;
private OperationUpdateRequest updateRequest;
@BeforeEach
void setUp() {
category = new Category();
category.setId(1L);
operation = new Operation();
operation.setId(1L);
operation.setPublicId("test-public-id");
operation.setName("Test Operation");
operation.setAmount(BigDecimal.valueOf(100));
operation.setType(OperationType.DEPOSIT);
operation.setCategory(category);
operationResponse = new OperationResponse();
operationResponse.setPublicId("test-public-id");
operationResponse.setName("Test Operation");
operationResponse.setAmount(BigDecimal.valueOf(100));
operationResponse.setType(OperationType.DEPOSIT);
operationResponse.setCategoryId(1L);
createRequest = new OperationCreateRequest();
createRequest.setName("Test Operation");
createRequest.setAmount(BigDecimal.valueOf(100));
createRequest.setType(OperationType.DEPOSIT);
createRequest.setCategoryId(1L);
updateRequest = new OperationUpdateRequest();
updateRequest.setName("Updated Operation");
updateRequest.setAmount(BigDecimal.valueOf(200));
updateRequest.setType(OperationType.WITHDRAWAL);
updateRequest.setCategoryId(1L);
}
@Test
void createOperation_Success() {
// Arrange
when(categoryRepository.findById(1L)).thenReturn(Optional.of(category));
when(operationMapper.toEntity(createRequest)).thenReturn(operation);
when(operationRepository.save(any(Operation.class))).thenReturn(operation);
when(operationMapper.toDto(operation)).thenReturn(operationResponse);
// Act
OperationResponse result = operationService.createOperation(createRequest);
// Assert
assertThat(result).isNotNull();
assertThat(result.getPublicId()).isEqualTo("test-public-id");
verify(categoryRepository).findById(1L);
verify(operationRepository).save(any(Operation.class));
}
@Test
void createOperation_CategoryNotFound() {
// Arrange
when(categoryRepository.findById(1L)).thenReturn(Optional.empty());
// Act & Assert
assertThrows(CategoryNotFoundException.class,
() -> operationService.createOperation(createRequest));
verify(operationRepository, never()).save(any(Operation.class));
}
@Test
void updateOperation_Success() {
// Arrange
when(operationRepository.findByPublicId("test-public-id")).thenReturn(Optional.of(operation));
when(categoryRepository.getReferenceById(1L)).thenReturn(category);
when(operationRepository.save(any(Operation.class))).thenReturn(operation);
when(operationMapper.toDto(operation)).thenReturn(operationResponse);
// Act
OperationResponse result = operationService.updateOperation("test-public-id", updateRequest);
// Assert
assertThat(result).isNotNull();
verify(operationRepository).save(any(Operation.class));
}
@Test
void updateOperation_NotFound() {
// Arrange
when(operationRepository.findByPublicId("test-public-id")).thenReturn(Optional.empty());
// Act & Assert
assertThrows(RuntimeException.class,
() -> operationService.updateOperation("test-public-id", updateRequest));
verify(operationRepository, never()).save(any(Operation.class));
}
@Test
void findAll_Success() {
// Arrange
PageRequest pageRequest = PageRequest.of(0, 10);
List operations = List.of(operation);
Page operationPage = new PageImpl<>(operations, pageRequest, 1);
when(operationRepository.findAll(pageRequest)).thenReturn(operationPage);
when(operationMapper.toDto(operation)).thenReturn(operationResponse);
// Act
PageResponse result = operationService.findAll(pageRequest);
// Assert
assertThat(result).isNotNull();
assertThat(result.getContent()).hasSize(1);
assertThat(result.getTotalElements()).isEqualTo(1);
}
@Test
void findByPublicId_Success() {
// Arrange
when(operationRepository.findByPublicId("test-public-id")).thenReturn(Optional.of(operation));
when(operationMapper.toDto(operation)).thenReturn(operationResponse);
// Act
OperationResponse result = operationService.findByPublicId("test-public-id");
// Assert
assertThat(result).isNotNull();
assertThat(result.getPublicId()).isEqualTo("test-public-id");
}
@Test
void findByPublicId_NotFound() {
// Arrange
when(operationRepository.findByPublicId("test-public-id")).thenReturn(Optional.empty());
// Act & Assert
assertThrows(RuntimeException.class,
() -> operationService.findByPublicId("test-public-id"));
}
@Test
void deleteOperation_Success() {
// Arrange
when(operationRepository.findByPublicId("test-public-id")).thenReturn(Optional.of(operation));
// Act
operationService.deleteOperation("test-public-id");
// Assert
verify(operationRepository).delete(operation);
}
@Test
void deleteOperation_NotFound() {
// Arrange
when(operationRepository.findByPublicId("test-public-id")).thenReturn(Optional.empty());
// Act & Assert
assertThrows(RuntimeException.class,
() -> operationService.deleteOperation("test-public-id"));
verify(operationRepository, never()).delete(any(Operation.class));
}
@Test
void updateOperation_PartialUpdate() {
// Arrange
OperationUpdateRequest partialRequest = new OperationUpdateRequest();
partialRequest.setName("Updated Name"); // Only update name
when(operationRepository.findByPublicId("test-public-id")).thenReturn(Optional.of(operation));
when(operationRepository.save(any(Operation.class))).thenReturn(operation);
when(operationMapper.toDto(operation)).thenReturn(operationResponse);
// Act
OperationResponse result = operationService.updateOperation("test-public-id", partialRequest);
// Assert
assertThat(result).isNotNull();
verify(operationRepository).save(any(Operation.class));
verify(categoryRepository, never()).getReferenceById(any()); // Category should not be updated
}
}
Тесты проходят успешно, но есть нюансы.
В тесте createOperation_Success
дублируются verify
и when
.
Почему это плохо?
when(...)
уже проверяет вызов метода.
verify(...)
имеет смысл только если проверяются все вызовы в createOperation
.
Как лучше?
Либо использовать verify
только на void
методах.
Либо проверять все вызовы, а не только часть.
Тест не проверяет содержимое объекта, только его тип.
when(operationRepository.save(any(Operation.class))).thenReturn(operation);
Это значит, что если внутри createOperation
объект Operation
изменится, тест этого не заметит.
Пример ошибки:
В коде забыли проставить категорию перед сохранением.
Тест всё равно проходит.
@Transactional
public OperationResponse createOperation(OperationCreateRequest request) {
final var category = categoryRepository.findById(request.getCategoryId())
.orElseThrow(() -> new CategoryNotFoundException(
String.format("Category with id %d not found", request.getCategoryId())));
final var operation = operationMapper.toEntity(request);
// operation.setCategory(category); // ОШИБКА!
final var savedOperation = operationRepository.save(operation);
return operationMapper.toDto(savedOperation);
}
Тест не обнаружит эту ошибку! Как исправить? Использовать ArgumentCaptor
, чтобы перехватить объект, переданный в save()
.
@Captor
ArgumentCaptor operationArgumentCaptor;
@Test
void createOperation_Success() {
// Arrange
when(categoryRepository.findById(1L)).thenReturn(Optional.of(category));
when(operationMapper.toEntity(createRequest)).thenReturn(operation);
when(operationRepository.save(any(Operation.class))).thenReturn(operation);
when(operationMapper.toDto(operation)).thenReturn(operationResponse);
// Act
OperationResponse result = operationService.createOperation(createRequest);
// Assert
assertThat(result).isNotNull();
assertThat(result.getPublicId()).isEqualTo("test-public-id");
// Перехватываем переданный в save объект
//captor можно вставить в verify блоке или в when блоке и там и там он отработает корректно
verify(operationRepository).save(operationArgumentCaptor.capture());
assertEquals(1, operationArgumentCaptor.getAllValues().size());
// Проверяем, что категория была установлена
var savedOperation = operationArgumentCaptor.getValue();
assertEquals(category, savedOperation.getCategory());
}
Теперь тест проверяет корректность объекта, а не просто его тип.
Рассмотрим всё тот же тест createOperation_Success()
. Как createOperation()
работает в коде:
Ищет категорию в БД
Маппит OperationCreateRequest
в Operation
Устанавливает категорию в объект Operation
Сохраняет объект Operaion
В тесте объект Operation
создаётся один раз в setUp()
– но уже с установленной категорией. Затем этот объект возвращается в моке
when(operationMapper.toEntity(createRequest)).thenReturn(operation);
Данная реализация снижает качество теста: часть логики createOperation()
не проверяется. В будущем это может привести к багам, которые останутся незамеченными.
Тестируемый класс назван operationService
вместо sut
(System Under Test), по второму варианту сразу видно, какой сервис тестируется
Генерация тестовых данных вынесена в @BeforeEach
, но не все данные нужны в каждом тесте. Лучше вынести в приватный метод и вызывать в нужных местах.
private OperationCreateRequest buildCreateRequest() {
return new OperationCreateRequest("Тест", 100.0, "WITHDRAW", 1L);
}
Так тесты будут чище и понятнее.
PR с unit тестами
https://github.com/nzinovev/anthropic-claude/pull/10
Юнит-тесты готовы, теперь попросил Клода написать MVC-тесты для контроллера.
Запрос к КлодуWrite a MVC-tests for OperationController. This is a reminder of what code is contained in the OperationController
@RestController
@RequestMapping("/api/v1/operations")
@RequiredArgsConstructor
public class OperationController {
private final OperationService operationService;
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public OperationResponse createOperation(@Valid @RequestBody OperationCreateRequest request) {
return operationService.createOperation(request);
}
@PutMapping("/{publicId}")
public OperationResponse updateOperation(
@PathVariable String publicId,
@RequestBody OperationUpdateRequest request) {
return operationService.updateOperation(publicId, request);
}
@GetMapping
public PageResponse<OperationResponse> findAll(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size,
@RequestParam(defaultValue = "publicId") String sortBy,
@RequestParam(defaultValue = "DESC") Sort.Direction direction) {
final var pageRequest = PageRequest.of(
page,
size,
Sort.by(direction, sortBy)
);
return operationService.findAll(pageRequest);
}
@GetMapping("/{publicId}")
public OperationResponse findByPublicId(@PathVariable String publicId) {
return operationService.findByPublicId(publicId);
}
@DeleteMapping("/{publicId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteOperation(@PathVariable String publicId) {
operationService.deleteOperation(publicId);
}
}
@Data
public class OperationResponse {
private String publicId;
private String name;
private BigDecimal amount;
private OperationType type;
private Long categoryId;
}
@Data
public class OperationCreateRequest {
@NotBlank(message = "Operation name is required")
@Size(max = 255, message = "Operation name must not exceed 255 characters")
private String name;
@NotNull(message = "Amount is required")
@Positive(message = "Amount must be positive")
@Digits(integer = 20, fraction = 2, message = "Amount must have at most 20 digits and 2 decimal places")
private BigDecimal amount;
@NotNull(message = "Operation type is required")
private OperationType type;
@NotNull(message = "Category ID is required")
@Positive(message = "Category ID must be positive")
private Long categoryId;
}
@Data
public class OperationUpdateRequest {
private String name;
private BigDecimal amount;
private OperationType type;
private Long categoryId;
}
I'll write comprehensive MVC tests for the OperationController using Spring's MockMvc:
// src/test/java/com/yourcompany/controller/OperationControllerTest.java
package com.yourcompany.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yourcompany.domain.entity.OperationType;
import com.yourcompany.dto.*;
import com.yourcompany.exception.CategoryNotFoundException;
import com.yourcompany.service.OperationService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.math.BigDecimal;
import java.util.List;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(OperationController.class)
class OperationControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
private OperationService operationService;
private OperationCreateRequest createRequest;
private OperationUpdateRequest updateRequest;
private OperationResponse operationResponse;
private PageResponse pageResponse;
@BeforeEach
void setUp() {
createRequest = new OperationCreateRequest();
createRequest.setName("Test Operation");
createRequest.setAmount(BigDecimal.valueOf(100));
createRequest.setType(OperationType.DEPOSIT);
createRequest.setCategoryId(1L);
updateRequest = new OperationUpdateRequest();
updateRequest.setName("Updated Operation");
updateRequest.setAmount(BigDecimal.valueOf(200));
updateRequest.setType(OperationType.WITHDRAWAL);
updateRequest.setCategoryId(2L);
operationResponse = new OperationResponse();
operationResponse.setPublicId("test-public-id");
operationResponse.setName("Test Operation");
operationResponse.setAmount(BigDecimal.valueOf(100));
operationResponse.setType(OperationType.DEPOSIT);
operationResponse.setCategoryId(1L);
pageResponse = new PageResponse<>();
pageResponse.setContent(List.of(operationResponse));
pageResponse.setPageNumber(0);
pageResponse.setPageSize(20);
pageResponse.setTotalElements(1);
pageResponse.setTotalPages(1);
pageResponse.setLast(true);
}
@Test
void createOperation_Success() throws Exception {
when(operationService.createOperation(any(OperationCreateRequest.class)))
.thenReturn(operationResponse);
mockMvc.perform(post("/api/v1/operations")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(createRequest)))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.publicId").value("test-public-id"))
.andExpect(jsonPath("$.name").value("Test Operation"))
.andExpect(jsonPath("$.amount").value(100))
.andExpect(jsonPath("$.type").value("DEPOSIT"))
.andExpect(jsonPath("$.categoryId").value(1));
verify(operationService).createOperation(any(OperationCreateRequest.class));
}
@Test
void createOperation_ValidationFailure() throws Exception {
createRequest.setName("");
createRequest.setAmount(BigDecimal.valueOf(-100));
createRequest.setType(null);
createRequest.setCategoryId(null);
mockMvc.perform(post("/api/v1/operations")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(createRequest)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.errors").isArray())
.andExpect(jsonPath("$.errors.length()").value(4));
verify(operationService, never()).createOperation(any());
}
@Test
void createOperation_CategoryNotFound() throws Exception {
when(operationService.createOperation(any()))
.thenThrow(new CategoryNotFoundException("Category not found"));
mockMvc.perform(post("/api/v1/operations")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(createRequest)))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.errors[0].message").value("Category not found"));
}
@Test
void updateOperation_Success() throws Exception {
when(operationService.updateOperation(eq("test-public-id"), any(OperationUpdateRequest.class)))
.thenReturn(operationResponse);
mockMvc.perform(put("/api/v1/operations/test-public-id")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(updateRequest)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.publicId").value("test-public-id"));
verify(operationService).updateOperation(eq("test-public-id"), any(OperationUpdateRequest.class));
}
@Test
void updateOperation_NotFound() throws Exception {
when(operationService.updateOperation(eq("non-existent-id"), any()))
.thenThrow(new RuntimeException("Operation not found"));
mockMvc.perform(put("/api/v1/operations/non-existent-id")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(updateRequest)))
.andExpect(status().isInternalServerError());
}
@Test
void findAll_Success() throws Exception {
PageRequest expectedPageRequest = PageRequest.of(0, 20, Sort.by(Sort.Direction.DESC, "publicId"));
when(operationService.findAll(expectedPageRequest)).thenReturn(pageResponse);
mockMvc.perform(get("/api/v1/operations"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content.length()").value(1))
.andExpect(jsonPath("$.pageNumber").value(0))
.andExpect(jsonPath("$.pageSize").value(20))
.andExpect(jsonPath("$.totalElements").value(1));
verify(operationService).findAll(any(PageRequest.class));
}
@Test
void findAll_WithCustomPagination() throws Exception {
PageRequest customPageRequest = PageRequest.of(1, 10, Sort.by(Sort.Direction.ASC, "name"));
when(operationService.findAll(any(PageRequest.class))).thenReturn(pageResponse);
mockMvc.perform(get("/api/v1/operations")
.param("page", "1")
.param("size", "10")
.param("sortBy", "name")
.param("direction", "ASC"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
verify(operationService).findAll(any(PageRequest.class));
}
@Test
void findByPublicId_Success() throws Exception {
when(operationService.findByPublicId("test-public-id")).thenReturn(operationResponse);
mockMvc.perform(get("/api/v1/operations/test-public-id"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.publicId").value("test-public-id"));
verify(operationService).findByPublicId("test-public-id");
}
@Test
void findByPublicId_NotFound() throws Exception {
when(operationService.findByPublicId("non-existent-id"))
.thenThrow(new RuntimeException("Operation not found"));
mockMvc.perform(get("/api/v1/operations/non-existent-id"))
.andExpect(status().isInternalServerError());
}
@Test
void deleteOperation_Success() throws Exception {
doNothing().when(operationService).deleteOperation("test-public-id");
mockMvc.perform(delete("/api/v1/operations/test-public-id"))
.andExpect(status().isNoContent());
verify(operationService).deleteOperation("test-public-id");
}
@Test
void deleteOperation_NotFound() throws Exception {
doThrow(new RuntimeException("Operation not found"))
.when(operationService).deleteOperation("non-existent-id");
mockMvc.perform(delete("/api/v1/operations/non-existent-id"))
.andExpect(status().isInternalServerError());
}
}
Все тесты проходят, кроме трёх:
findByPublicId_NotFound
deleteOperation_NotFound
updateOperation_NotFound
И здесь очень интересны момент, Клод ожидает, что сервис вернёт 500-ю ошибку, но на самом деле он должен отвечать 404-й (и название теста на это намекает). Тест изначально не проходил, потому что Клод никогда не добавлял обработку 500-х ошибок в ExceptionHandler
. Если бы тест ожидал 404-ю ошибку, то он всё равно бы падал, т.к. при обработке валидации, была добавлена обработка лишь CategoryNotFoundException
, но есть и плюс: тесты падают, а значит, их нельзя игнорировать. Это даёт шанс исправить ошибку вовремя.
Исправляем: добавляем общее исключение NotFoundException
и обработку RuntimeException
:
@ExceptionHandler(NotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ValidationErrorResponse handleCategoryNotFoundException(NotFoundException ex) {
ValidationErrorResponse response = new ValidationErrorResponse();
response.getErrors().add(new ValidationErrorResponse.ValidationError(
"id",
ex.getMessage()
));
return response;
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorDto handleException(Exception ex) {
return new ErrorDto(ex.getMessage());
}
private record ErrorDto(String message){}
Теперь сервис корректно возвращает 404 в случае отсутствия объекта.
Клод в целом справился, но есть несколько проблем. Тест createOperation_Success
проверяет результат через jsonPath
:
.andExpect(jsonPath("$.publicId").value("test-public-id"))
.andExpect(jsonPath("$.name").value("Test Operation"))
.andExpect(jsonPath("$.amount").value(100))
Проверки через jsonPath неудобны, их тяжелее читать, особенно если в модели присутствует вложенность или массив данных. Вариант с хранением json-файла с ожидаемым результатом будет намного проще.
Тест findAll_WithCustomPagination
совсем не проверяет возвращаемый результат, он подготовлен, но не добавлен в проверку. findByPublicId_Success
проверяет лишь id
, хотя в beforeEach
подготовлен целый объект.
На первый взгляд может показаться, что Клод справился хорошо но если приглядеться– не хватает проверок, а кое-где они просто отсутствуют.
PR с MVC тестами
https://github.com/nzinovev/anthropic-claude/pull/11
Мы прошли по минимальному циклу разработки, используя ИИ:
Создали БД, таблицы и сущности.
Реализовали взаимодействие с БД через Spring JPA.
Написали CRUD-операции и обработку ошибок.
Добавили юнит-тесты и MVC-тесты.
Да, он выдал рабочий код, который можно было довести до финального состояния.
Нет. Код требовал исправлений и доработок. Клод допускал ошибки, которые опытный разработчик избежал бы сразу.
Нет, Клод справился только потому, что получал чёткие требования и правильную настройку. ИИ может писать код, но только если задающий понимает, что именно он хочет получить.
Можно ли использовать ИИ в реальной работе?
Не просто можно, а нужно. ИИ существенно ускоряет разработку, но важно понимать его ограничения.
Если промпт размытый – код будет неполным или некорректным.
Если задать чёткий запрос – модель выдаст качественный код.
ИИ отлично справляется с небольшими блоками кода.
Если же попросить его реализовать большой модуль, он может упустить детали или перегрузить выдачу.
ИИ эффективен как помощник в написании небольших фрагментов кода, но для сложных задач нужен контроль со стороны разработчика.
ИИ не гарантирует идеальный код и может нести потенциальные риски.
ИИ не анализирует код так, как разработчик. Он может допустить ошибки, которые незаметны сразу, но в будущем приведут к багам.
При работе с большими моделям такими как ChatGPT и Claude нужно быть осторожными с чувствительными и корпоративными данными. Передача кода в публичные LLM-модели несёт риск утечки данных.
Не отправляйте коммерчески важный код в LLM. Если же вы это делаете, обязательно анонимизируйте данные перед отправкой.
Если конфиденциальность критична и анонимизация затруднена, рассмотрите вариант использования локальных LLM. Большой плюс таких моделей, что они локальны, следовательно, ваши данные не покинут просторы вашего ПК.
Код от ИИ бывает отличным, средним или откровенно плохим. Качество зависит не от модели, а от того, кто задаёт вопросы. ИИ – мощный инструмент, но бездумное использование может привести к проблемам.
Когда-то – возможно. Но точно не сейчас.
ИИ пишет код, но не понимает его так, как человек.
Самая сложная часть разработки – архитектура, бизнес-логика, работа с требованиями – остаются за человеком.
ИИ – не замена, а мощный инструмент. Разработчик, который использует ИИ, будет писать код быстрее, эффективнее и качественнее. Разработчик, который не использует ИИ, будет работать дольше и медленнее.
ИИ – это не враг, а помощник, который ускоряет работу и снимает рутину.
Мы рассмотрели три из четырёх поставленных вопросов. Осталось ответить на последний, какая модель лучше для программирования? Anthropic Claude? ChatGPT? DeepSeek?
Об этом – в следующей части.
Оставайтесь на связи.
p.s. Если было интересно, заходи https://t.me/nizeEcho