This project is a Spring Boot application that connects to a SQL Server database, provides a REST API to manage users, and includes a stored procedure for user creation. The stored procedure is tested using tSQLt, a unit testing framework for SQL Server.
spring-boot-sqlserver-tsqlt-demo/
βββ src/
β βββ main/
β β βββ java/ua/lviv/javaclub/tsqlt/
β β β βββ TaskEstimationApplication.java
β β β βββ controller/
β β β β βββ UserController.java
β β β βββ entity/
β β β β βββ User.java
β β β βββ repository/
β β β β βββ UserRepository.java
β β β βββ service/
β β β β βββ UserService.java
β β βββ resources/
β β β βββ application.yml
β β β βββ schema.sql
β β β βββ create-user.http
β β β βββ get-users.http
β βββ test/
β β βββ resources/
β β β βββ FacadeDacpacs/
β β β β βββ tSQLtFacade.*.dacpac (various versions)
β β β βββ PrepareServer.sql
β β β βββ my_tests.sql
β β β βββ run.sql
β β β βββ tSQLt.class.sql
β β β βββ tSQLt.print.sql
βββ docker-compose.yml
βββ build.gradle
βββ README.md
- Java 17+
- Gradle
- Docker (for SQL Server)
docker-compose up -dThis starts SQL Server on localhost:1433.
spring:
datasource:
url: jdbc:sqlserver://localhost:1433;databaseName=master;encrypt=false;trustServerCertificate=true
username: sa
password: YourStrong!Passw0rd
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
jpa:
show-sql: true
database-platform: org.hibernate.dialect.SQLServerDialect
hibernate:
ddl-auto: none
server:
port: 8080./gradlew bootRunCREATE OR ALTER PROCEDURE dbo.CreateUser @Name NVARCHAR(255),
@Email NVARCHAR(255)
AS
BEGIN
DECLARE @isEmailExist BIT,
@Score INT;
SET @isEmailExist = dbo.isEmailExist(@Email);
IF (@isEmailExist = 0)
BEGIN
EXEC dbo.getScore @Email, @res = @Score OUTPUT
INSERT INTO dbo.Users (name, email, score)
VALUES (@Name, @Email, @Score);
END
END;
GO
CREATE OR ALTER FUNCTION dbo.isEmailExist(
@Email NVARCHAR(255)
) RETURNS BIT
AS
BEGIN
RETURN IIF(@Email = 'email100500', 1, 0);
END;
GO
CREATE OR ALTER PROCEDURE dbo.getScore @Email NVARCHAR(255), @res INT OUTPUT
AS
BEGIN
set @res = 100
END;
GO| Method | Endpoint | Description |
|---|---|---|
GET |
/users |
Fetch all users |
POST |
/users |
Create a new user via stored procedure |
{
"name": "John Doe",
"email": "john@example.com"
}-- Run all tests across all test classes
EXEC tSQLt.RunAll;
GO
-- Run all tests in TestUser class
EXEC tSQLt.Run 'TestUser';
GO
-- Run a specific test
EXEC tSQLt.Run 'TestUser.[test CreateUser inserts row with FakeTable]';
GOMIT License. Free to use and modify!
β You're all set! Now, start coding π.