-
Notifications
You must be signed in to change notification settings - Fork 4
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
6 changed files
with
161 additions
and
0 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
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> |
16 changes: 16 additions & 0 deletions
16
sprint-boot/src/main/java/com/github/seanyinx/testbed/GlobalExceptionHandler.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,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
11
sprint-boot/src/main/java/com/github/seanyinx/testbed/MainApp.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,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); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
sprint-boot/src/main/java/com/github/seanyinx/testbed/UserController.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,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)); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
sprint-boot/src/test/java/com/github/seanyinx/testbed/Session1_ControllerTest.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,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)); | ||
} | ||
} |