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

Test/student #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/example/demo/student/StudentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.example.demo.student.exception.BadRequestException;
import com.example.demo.student.exception.StudentNotFoundException;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/example/demo/student/StudentRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.demo.student;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import static org.junit.jupiter.api.Assertions.*;

@DataJpaTest
class StudentRepositoryTest {

@Autowired
StudentRepository underTest;

@AfterEach
void tearDown() {
underTest.deleteAll();
}

@Test
void shouldExistStudentsEmail() {

String email = "[email protected]";
Student student = new Student("amir"
, email
, Gender.MALE);

underTest.save(student);
boolean result = underTest.selectExistsEmail(email);
assertTrue(result);
}

@Test
void shouldNotExistStudentsEmail() {

String email = "[email protected]";
String email2 = "[email protected]";
Student student = new Student("amir"
, email
, Gender.MALE);

underTest.save(student);
boolean result = underTest.selectExistsEmail(email2);
assertFalse(result);
}

}
108 changes: 108 additions & 0 deletions src/test/java/com/example/demo/student/StudentServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.example.demo.student;

import com.example.demo.student.exception.BadRequestException;
import com.example.demo.student.exception.StudentNotFoundException;
import net.bytebuddy.matcher.ElementMatchers;
import org.assertj.core.api.Assertions;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.List;

import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class StudentServiceTest {

@Mock
private StudentRepository studentRepository;
private StudentService underTest;
private Student student;

@BeforeEach
void setUp() {
underTest = new StudentService(studentRepository);
student = new Student("amir", "[email protected]", Gender.MALE);
}


@Test
void canGetAllStudents() {
// Arrange
List<Student> studentList = Arrays.asList(
new Student("amir", "[email protected]", Gender.MALE),
new Student("sara", "[email protected]", Gender.FEMALE));
when(studentRepository.findAll()).thenReturn(studentList);

// Act
List<Student> allStudents = underTest.getAllStudents();

// Assert
Assertions.assertThat(studentList.size()).isEqualTo(allStudents.size());

// Verify
Mockito.verify(studentRepository).findAll();
}

@Test
void shouldAddStudent() {

underTest.addStudent(student);

ArgumentCaptor<Student> studentArgumentCaptor = ArgumentCaptor.forClass(Student.class);

Mockito.verify(studentRepository).save(studentArgumentCaptor.capture());

Student value = studentArgumentCaptor.getValue();

Assertions.assertThat(value).isEqualTo(student);


}

@Test
void shouldGetExceptionWhenAddingStudent() {

when(studentRepository.selectExistsEmail(ArgumentMatchers.any())).thenReturn(true);

Assertions.assertThatThrownBy(() -> underTest.addStudent(student))
.isInstanceOfAny(BadRequestException.class);
Mockito.verify(studentRepository, Mockito.never()).save(ArgumentMatchers.any());
}

@Test
void shouldBeAbleToDeleteStudent() {
Long studentId = 1L;

when(studentRepository.existsById(studentId)).thenReturn(true);

underTest.deleteStudent(studentId);

verify(studentRepository, times(1)).deleteById(studentId);

}

@Test
void shouldGetExceptionWhenDeleteStudent() {

when(studentRepository
.existsById(ArgumentMatchers.any()))
.thenReturn(false);

Assertions
.assertThatThrownBy(() -> underTest.deleteStudent(121L))
.isInstanceOf(StudentNotFoundException.class);

Mockito
.verify(studentRepository, Mockito.never()).deleteById(ArgumentMatchers.any());
}
}
7 changes: 7 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true