스프링 부트 REST 컨트롤러에 대해 JUnit 테스트를 실행하는 중 예외가 발생합니다. Postman을 통해 API를 테스트했으며 예상대로 작동합니다. JUnit 테스트에서 내가 무엇을 놓치고 있는지 확실하지 않습니다.스프링 부트의 REST API에 대한 JUnit 테스트에 실패했습니다.
ProductController.java
@RestController
@RequestMapping("/api")
public class ProductController {
@Inject
private ProductRepository productRepository;
//URI: http://localhost:8080/api/products/50
@RequestMapping(value = "/products/{productId}", method = RequestMethod.GET)
public ResponseEntity<?> getProduct(@PathVariable Long productId) {
verifyProductExists(productId);
Product product = productRepository.findOne(productId);
return new ResponseEntity<>(product, HttpStatus.OK);
}
protected void verifyProductExists(Long productId) throws ResourceNotFoundException {
Product product = productRepository.findOne(productId);
if (product == null) {
throw new ResourceNotFoundException("Product with id " + productId + " not found...");
}
}
}
ResourceNotFoundException.java
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;
public ResourceNotFoundException() {
}
public ResourceNotFoundException(String message) {
super(message);
}
public ResourceNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}
이죠 스루 :
http://localhost:8080/api/products/1 -> Returns 200 with Product data in JSON format
http://localhost:8080/api/products/999 -> Returns 404 with Exception data in JSON format
ProductRestClientTest.java
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductRestClientTest {
static final String VALID_PRODUCT_API_URI = "http://localhost:8080/api/products/35";
static final String INVALID_PRODUCTS_API_URI = "http://localhost:8080/api/products/555";
private RestTemplate restTemplate;
@Before
public void setUp() {
restTemplate = new RestTemplate();
}
/*
Testing Happy Path scenario
*/
@Test
public void testProductFound() {
ResponseEntity<?> responseEntity = restTemplate.getForEntity(VALID_PRODUCT_API_URI, Product.class);
assert (responseEntity.getStatusCode() == HttpStatus.OK);
}
/*
Testing Error scenario
*/
@Test(expected = ResourceNotFoundException.class)
public void testProductNotFound() {
ResponseEntity<?> responseEntity = restTemplate.getForEntity(INVALID_PRODUCTS_API_URI, Product.class);
assert (responseEntity.getStatusCode() == HttpStatus.NOT_FOUND);
}
@After
public void tearDown() {
restTemplate = null;
}
}
예외는 점에서는 예외가 있지만 바디와 포함 된 HTTP 메시지 HTTP를 반환하지 않는 것입니다
Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.759 sec <<< FAILURE! - in com.study.spring.boot.rest.ProductRestClientTest
testProductNotFound(com.study.spring.boot.rest.ProductRestClientTest) Time elapsed: 0.46 sec <<< ERROR!
java.lang.Exception: Unexpected exception, expected<com.study.spring.boot.rest.ResourceNotFoundException> but was<org.springframework.web.client.HttpClientErrorException>
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:312)
at com.study.spring.boot.rest.ProductRestClientTest.testProductNotFound(ProductRestClientTest.java:42)
당신이 @IntegrationTest와 테스트 클래스에 주석을 시도? –