Skip to content

Latest commit

Β 

History

History
149 lines (126 loc) Β· 3.74 KB

File metadata and controls

149 lines (126 loc) Β· 3.74 KB

Spring Boot + SQL Server + tSQLt Demo

πŸ“Œ Project Overview

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.

πŸ“ Project Structure

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

πŸ›  Setup & Installation

1️⃣ Prerequisites

  • Java 17+
  • Gradle
  • Docker (for SQL Server)

2️⃣ Start SQL Server with Docker

docker-compose up -d

This starts SQL Server on localhost:1433.

3️⃣ Configure Database in application.yml

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

4️⃣ Run the Application

./gradlew bootRun

πŸ›  Stored Procedure (in schema.sql)

CREATE 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

πŸš€ REST API Endpoints

Method Endpoint Description
GET /users Fetch all users
POST /users Create a new user via stored procedure

Example Request (POST)

{
  "name": "John Doe",
  "email": "john@example.com"
}

Run tSQLt Tests

-- 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]';
GO

πŸ“œ License

MIT License. Free to use and modify!


βœ… You're all set! Now, start coding πŸš€.