Skip to content

Commit

Permalink
Add endpoint that throws test exception (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
yertsti authored Oct 10, 2023
1 parent 1373ced commit d1ee03e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package edu.hawaii.its.api.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.hawaii.its.api.exception.ExceptionForTesting;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/groupings/v2.1/testing")
public class GroupingsRestControllerv2_1ForTesting {

private static final Log logger = LogFactory.getLog(GroupingsRestControllerv2_1ForTesting.class);

@GetMapping(value = "/exception")
@ResponseBody
public ResponseEntity throwException() {
logger.debug("Entered REST throwException");
throw new ExceptionForTesting("Test exception");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package edu.hawaii.its.api.exception;

public class ExceptionForTesting extends RuntimeException {
public ExceptionForTesting(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package edu.hawaii.its.api.controller;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import edu.hawaii.its.api.configuration.SpringBootWebApplication;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@SpringBootTest(classes = { SpringBootWebApplication.class })
public class GroupingsRestControllerv2_1ForTestingTest {

private static final String API_BASE = "/api/groupings/v2.1/testing";
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;

@BeforeEach
public void setUp() {
mockMvc = webAppContextSetup(context).build();
}

@Test
public void throwExceptionTest() throws Exception {
assertNotNull(mockMvc.perform(get(API_BASE + "/exception"))
.andExpect(status().isInternalServerError()));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package edu.hawaii.its.api.exception;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class ExceptionForTestingTest {

@Test
public void construction() {
ExceptionForTesting ex = new ExceptionForTesting("Test Exception");
assertNotNull(ex);
assertThat(ex.getMessage(), equalTo("Test Exception"));
}
}

0 comments on commit d1ee03e

Please sign in to comment.