-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring and add integration tests
Co-authored-by: skyliam <liamskydamien@gmail.com>
- Loading branch information
1 parent
42d01f0
commit 3a60d49
Showing
4 changed files
with
169 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
api/src/test/java/com/bjs/bjsapi/controllers/StudentScoreControllerIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.bjs.bjsapi.controllers; | ||
|
||
import static org.mockito.Mockito.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import java.util.Collections; | ||
import java.util.Optional; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import com.bjs.bjsapi.database.model.SportResult; | ||
import com.bjs.bjsapi.database.model.Student; | ||
import com.bjs.bjsapi.database.model.enums.DisciplineType; | ||
import com.bjs.bjsapi.database.repository.SportResultRepository; | ||
import com.bjs.bjsapi.database.repository.StudentRepository; | ||
import com.bjs.bjsapi.helper.CalculationInformationService; | ||
|
||
@RunWith(SpringRunner.class) | ||
@WebMvcTest(controllers = StudentScoreController.class) | ||
public class StudentScoreControllerIntegrationTest { | ||
|
||
@MockBean | ||
public StudentRepository studentRepository; | ||
|
||
@MockBean | ||
public SportResultRepository sportResultRepository; | ||
|
||
@MockBean | ||
public CalculationInformationService calculationInformationService; | ||
|
||
@Autowired | ||
public MockMvc mockMvc; | ||
|
||
@Test | ||
public void test_calculateScore_found() throws Exception { | ||
Student student = new Student(); | ||
student.setFemale(true); | ||
SportResult sportResult = new SportResult(); | ||
|
||
sportResult.setDiscipline(DisciplineType.RUN_50); | ||
sportResult.setResult(7.00F); | ||
|
||
doReturn(Collections.singletonList(sportResult)).when(sportResultRepository).findByStudent(student); | ||
doReturn(Optional.of(student)).when(studentRepository).findById(1L); | ||
doReturn(3.79000).when(calculationInformationService).getAValue(true, DisciplineType.RUN_50); | ||
doReturn(0.00690).when(calculationInformationService).getCValue(true, DisciplineType.RUN_50); | ||
|
||
mockMvc.perform(get("/api/v1/students/{id}/score", 1) | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string(String.valueOf(451))); | ||
} | ||
|
||
@Test | ||
public void test_calculateScore_notFound() throws Exception { | ||
Student student = new Student(); | ||
student.setFemale(true); | ||
SportResult sportResult = new SportResult(); | ||
|
||
sportResult.setDiscipline(DisciplineType.RUN_50); | ||
sportResult.setResult(7.00F); | ||
|
||
doReturn(Collections.singletonList(sportResult)).when(sportResultRepository).findByStudent(student); | ||
doReturn(Optional.of(student)).when(studentRepository).findById(1L); | ||
doReturn(3.79000).when(calculationInformationService).getAValue(true, DisciplineType.RUN_50); | ||
doReturn(0.00690).when(calculationInformationService).getCValue(true, DisciplineType.RUN_50); | ||
|
||
mockMvc.perform(get("/api/v1/students/{id}/score", 2) | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters