Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of Slice Tests #189

Merged
merged 13 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ include('graphql')
include('jooq')
include('test-execution-listeners')
include('test-rest-assured')
include('test-slice-tests')
include('web-rest-client')
3 changes: 3 additions & 0 deletions test-slice-tests/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/gradlew text eol=lf
*.bat text eol=crlf
*.jar binary
37 changes: 37 additions & 0 deletions test-slice-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/
35 changes: 35 additions & 0 deletions test-slice-tests/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.4.1'
id 'io.spring.dependency-management' version '1.1.7'
}

group = 'zin.rashidi.boot'
version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-batch'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.boot:spring-boot-testcontainers'
testImplementation 'org.springframework.batch:spring-batch-test'
testImplementation 'org.testcontainers:junit-jupiter'
testImplementation 'org.testcontainers:postgresql'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
useJUnitPlatform()
}
1 change: 1 addition & 0 deletions test-slice-tests/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'test-slice-tests'
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package zin.rashidi.boot.test.slices;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestSliceTestsApplication {

public static void main(String[] args) {
SpringApplication.run(TestSliceTestsApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package zin.rashidi.boot.test.slices.user;

import jakarta.persistence.*;

import static zin.rashidi.boot.test.slices.user.User.Status.ACTIVE;

/**
* @author Rashidi Zin
*/
@Entity
@Table(
name = "users",
uniqueConstraints = @UniqueConstraint(name = "uniqueUsername", columnNames = "username")
)
class User {

@Id
@GeneratedValue
private Long id;

@Embedded
private Name name;
private String username;
private Status status;

protected User() {}

User(Name name, String username) {
this.name = name;
this.username = username;
this.status = ACTIVE;
}

@Embeddable
record Name(String first, String last) {}

enum Status {
ACTIVE, DORMANT
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package zin.rashidi.boot.test.slices.user;

import org.springframework.data.jpa.repository.JpaRepository;

/**
* @author Rashidi Zin
*/
interface UserRepository extends JpaRepository<User, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package zin.rashidi.boot.test.slices.user;

import org.springframework.web.bind.annotation.*;
import zin.rashidi.boot.test.slices.user.User.Name;

import static org.springframework.http.HttpStatus.CREATED;

/**
* @author Rashidi Zin
*/
@RestController
class UserResource {

private final UserRepository repository;

UserResource(UserRepository repository) {
this.repository = repository;
}

@PostMapping("/users")
@ResponseStatus(CREATED)
public User add(@RequestBody UserRequest request) {
return repository.save(new User(request.name(), request.username()));
}

@GetMapping("/users/{id}")
public User read(@PathVariable Long id) {
return repository.findById(id).orElseThrow();
}

record UserRequest(Name name, String username) {
}

}
1 change: 1 addition & 0 deletions test-slice-tests/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=test-slice-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package zin.rashidi.boot.test.slices;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;

@Import(TestcontainersConfiguration.class)
@SpringBootTest
class TestSliceTestsApplicationTests {
rashidi marked this conversation as resolved.
Show resolved Hide resolved

@Test
void contextLoads() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package zin.rashidi.boot.test.slices;

import org.springframework.boot.SpringApplication;

public class TestTestSliceTestsApplication {

public static void main(String[] args) {
SpringApplication.from(TestSliceTestsApplication::main).with(TestcontainersConfiguration.class).run(args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package zin.rashidi.boot.test.slices;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
class TestcontainersConfiguration {

@Bean
@ServiceConnection
PostgreSQLContainer<?> postgresContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("postgres:latest"));
}

}
Loading