-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/test/java/com/fighter/demo/controllers/FighterControllerTest.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,68 @@ | ||
package com.fighter.demo.controllers; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fighter.demo.models.dto.Fighter; | ||
import com.fighter.demo.repositories.FighterRepository; | ||
import com.fighter.demo.service.FighterService; | ||
import com.fighter.demo.service.FighterServiceImpl; | ||
import com.jayway.jsonpath.JsonPath; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.MockMvcResultMatchersDsl; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@Tag("controllers") | ||
@ExtendWith(SpringExtension.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@AutoConfigureMockMvc | ||
class FighterControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mvc; | ||
|
||
private ObjectMapper mapper = new ObjectMapper(); | ||
|
||
|
||
@Test | ||
void getAllFighters() throws Exception { | ||
MvcResult result = this.mvc.perform(MockMvcRequestBuilders.get("/fighters/")) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) | ||
.andReturn(); | ||
List<Fighter> fightersReceived = mapper.readValue(result.getResponse().getContentAsByteArray(), new TypeReference<>() { | ||
}); | ||
assertEquals(8, fightersReceived.size()); | ||
} | ||
|
||
@Test | ||
void getFighterById() throws Exception { | ||
MvcResult result = this.mvc.perform(MockMvcRequestBuilders.get("/fighters/"+2)) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) | ||
.andReturn(); | ||
Fighter fighterReceived = mapper.readValue(result.getResponse().getContentAsByteArray(), Fighter.class); | ||
assertNotNull(fighterReceived.getName()); | ||
|
||
this.mvc.perform(MockMvcRequestBuilders.get("/fighters/"+1234)) | ||
.andExpect(MockMvcResultMatchers.status().isNotFound()) | ||
.andReturn(); | ||
} | ||
} |
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
67 changes: 67 additions & 0 deletions
67
src/test/java/com/fighter/demo/models/dto/FighterTest.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,67 @@ | ||
package com.fighter.demo.models.dto; | ||
|
||
import com.fighter.demo.models.dto.Fighter; | ||
import com.fighter.demo.repositories.FighterRepository; | ||
import com.fighter.demo.service.FighterService; | ||
import com.fighter.demo.service.FighterServiceImpl; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@Tag("entity") | ||
class FighterTest { | ||
|
||
private FighterService fighterService; | ||
private FighterRepository fighterRepository; | ||
|
||
@BeforeEach | ||
void setUp(){ | ||
List<Fighter> fighters = new LinkedList<>(); | ||
for (int i = 0; i < 8; i++) { | ||
|
||
Fighter fighter = new Fighter(); | ||
fighter.setId(i); | ||
fighter.setName("fighter"+i); | ||
if(i < 2){ | ||
fighter.setWins((short) (10*i)); | ||
fighter.setLosses((short) (2*i)); | ||
} | ||
else{ | ||
fighter.setWins((short) (Math.random()*100)); | ||
fighter.setLosses((short) (Math.random()*100)); | ||
} | ||
|
||
fighter.calculateHealth(); | ||
fighters.add(fighter); | ||
} | ||
fighterRepository = Mockito.mock(FighterRepository.class); | ||
fighterService = Mockito.mock(FighterService.class); | ||
|
||
Mockito.when(fighterService.findAll()).thenReturn(fighters); | ||
Mockito.when(fighterService.findById("1")).thenReturn(fighters.get(1)); | ||
Mockito.when(fighterService.findById("12345")).thenReturn(null); | ||
|
||
} | ||
|
||
@Test | ||
void getAllFighters() { | ||
assertEquals(8, fighterService.findAll().size()); | ||
} | ||
|
||
@Test | ||
void getFighterById() { | ||
assertNotNull(fighterService.findById("1")); | ||
assertNull(fighterService.findById("12345")); | ||
} | ||
} |
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