我在运行针对Spring Boot REST控制器的JUnit测试时收到异常。我通过Postman测试了API,它按预期工作。不知道我在JUnit测试中缺少什么。Spring引导中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;
}
}
异常而上述JUnit测试运行
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来注释测试类? –