Skip to content

Commit

Permalink
added spring controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
seanyinx committed Jul 30, 2018
1 parent 2564b41 commit e841e87
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
<version>0.1.0-SNAPSHOT</version>
<modules>
<module>base</module>
<module>sprint-boot</module>
</modules>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>

<spring.boot.version>2.0.0.RELEASE</spring.boot.version>
<slf4j.version>1.7.25</slf4j.version>
<log4j.version>2.6.2</log4j.version>
<disruptor.version>3.3.6</disruptor.version>
Expand All @@ -25,6 +27,11 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.github.seanyinx</groupId>
<artifactId>base</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
Expand Down
52 changes: 52 additions & 0 deletions sprint-boot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>test-koan</artifactId>
<groupId>com.github.seanyinx</groupId>
<version>0.1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>sprint-boot</artifactId>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.github.seanyinx</groupId>
<artifactId>base</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.seanyinx.testbed;

import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(Exception.class)
ResponseEntity<String> handleException(Exception ex) {
return new ResponseEntity<>(INTERNAL_SERVER_ERROR);
}
}
11 changes: 11 additions & 0 deletions sprint-boot/src/main/java/com/github/seanyinx/testbed/MainApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.seanyinx.testbed;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.seanyinx.testbed;

import java.util.NoSuchElementException;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.seanyinx.testbed.base.domain.User;
import com.github.seanyinx.testbed.base.services.UserService;

@Controller
@RequestMapping("/users")
class UserController {

private final UserService userService;

@Autowired
public UserController(UserService userService) {
this.userService = userService;
}

@GetMapping("/{id}")
User getUser(@PathVariable long id) {
Optional<User> user = userService.findUser(id);

return user.orElseThrow(() -> new NoSuchElementException("No such user with id " + id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.seanyinx.testbed;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

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.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import com.github.seanyinx.testbed.base.domain.User;
import com.github.seanyinx.testbed.base.services.UserService;

@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class Session1_ControllerTest {
private final User jack = new User(1L, "jack");

@Autowired
private MockMvc mockMvc;

@MockBean
private UserService userService;

@Test
public void shouldReturnExpectedUser() throws Exception {
when(userService.findUser(1L)).thenReturn(Optional.of(jack));

mockMvc.perform(get("/users/{id}", 1));
}

@Test
public void shouldReturn500() throws Exception {
when(userService.findUser(1L)).thenReturn(Optional.empty());

mockMvc.perform(get("/users/{id}", 1));
}
}

0 comments on commit e841e87

Please sign in to comment.