Skip to content

added tests to student package #16

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

Open
wants to merge 1 commit 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
18 changes: 5 additions & 13 deletions src/test/java/com/example/demo/DemoApplicationTests.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package com.example.demo;

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

@SpringBootTest
class DemoApplicationTests {

@Test
void contextLoads() {
}

}
package com.example.demo;

//@SpringBootTest
class DemoApplicationTests {
}
53 changes: 53 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,53 @@
package com.example.demo.student;

import org.junit.jupiter.api.AfterEach;
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.orm.jpa.DataJpaTest;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;

@DataJpaTest
class StudentRepositoryTest {

@Autowired
private StudentRepository underTest;

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

@Test
void itShouldCheckIfStudentEmailExists() {
// given
String email = "[email protected]";
Student student = new Student(
"Mike",
email,
Gender.MALE
);
underTest.save(student);

// when
Boolean expected = underTest.selectExistsEmail(email);

// then
assertThat(expected).isTrue();
}

@Test
void itShouldCheckIfStudentEmailDoesNotExist() {
// given
String email = "[email protected]";

// when
Boolean expected = underTest.selectExistsEmail(email);


// then
assertThat(expected).isFalse();
}
}
83 changes: 83 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,83 @@
package com.example.demo.student;

import com.example.demo.student.exception.BadRequestException;
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.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
class StudentServiceTest {

@Mock
private StudentRepository studentRepository;
private StudentService underTest;

@BeforeEach
void setUp() {
underTest = new StudentService(studentRepository);
}

@Test
void canGetAllStudents() {
// when
underTest.getAllStudents();
// then
verify(studentRepository).findAll();
}

@Test
void canAddStudent() {
// given
Student student = new Student(
"Mike",
"[email protected]",
Gender.MALE
);
// when
underTest.addStudent(student);
// then
ArgumentCaptor<Student> studentArgumentCaptor =
ArgumentCaptor.forClass(Student.class);
verify(studentRepository)
.save(studentArgumentCaptor.capture());

Student capturedStudent = studentArgumentCaptor.getValue();
assertThat(capturedStudent).isEqualTo(student);
}

@Test
void willThrowWhenEmailIsTaken() {
// given
Student student = new Student(
"Mike",
"[email protected]",
Gender.MALE
);
given(studentRepository.selectExistsEmail(student.getEmail()))
.willReturn(true);

// when
// then
assertThatThrownBy(() -> underTest.addStudent(student))
.isInstanceOf(BadRequestException.class)
.hasMessageContaining("Email " + student.getEmail() + " taken");
verify(studentRepository, never()).save(any());
}

@Test
@Disabled
void canDeleteStudent() {

}
}
8 changes: 8 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring.datasource.url=jdbc:h2://mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgresSQLDialect
spring.jpa.properties.hibernate.format_sql=true