Skip to content

Commit

Permalink
adding controller tests - closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
springframeworkguru committed May 12, 2019
1 parent ef20d41 commit fa9009e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
Expand Down Expand Up @@ -65,6 +70,27 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package guru.springframework.msscbeerservice;

import org.junit.Test;
import org.junit.runner.RunWith;

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

@RunWith(SpringRunner.class)
@SpringBootTest
public class MsscBeerServiceApplicationTests {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package guru.springframework.msscbeerservice.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import guru.springframework.msscbeerservice.web.model.BeerDto;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.util.UUID;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(BeerController.class)
class BeerControllerTest {

@Autowired
MockMvc mockMvc;

@Autowired
ObjectMapper objectMapper;

@Test
void getBeerById() throws Exception {

mockMvc.perform(get("/api/v1/beer/" + UUID.randomUUID().toString()).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());

}

@Test
void saveNewBeer() throws Exception {

BeerDto beerDto = BeerDto.builder().build();
String beerDtoJson = objectMapper.writeValueAsString(beerDto);

mockMvc.perform(post("/api/v1/beer/")
.contentType(MediaType.APPLICATION_JSON)
.content(beerDtoJson))
.andExpect(status().isCreated());
}

@Test
void updateBeerById() throws Exception {
BeerDto beerDto = BeerDto.builder().build();
String beerDtoJson = objectMapper.writeValueAsString(beerDto);

mockMvc.perform(put("/api/v1/beer/" + UUID.randomUUID().toString())
.contentType(MediaType.APPLICATION_JSON)
.content(beerDtoJson))
.andExpect(status().isNoContent());
}
}

0 comments on commit fa9009e

Please sign in to comment.