Refactor Spring-boot-test examples (#1328)
* Refactor Spring-boot-test examples * Refactor Spring-boot-test examples
This commit is contained in:
parent
656afd6ff5
commit
250cb7c164
@ -1,58 +1,21 @@
|
|||||||
package org.baeldung.boot.components;
|
package org.baeldung.boot.components;
|
||||||
|
|
||||||
import org.baeldung.boot.exceptions.CommonException;
|
|
||||||
import org.baeldung.boot.exceptions.FooNotFoundException;
|
|
||||||
import org.baeldung.boot.model.Foo;
|
import org.baeldung.boot.model.Foo;
|
||||||
import org.baeldung.boot.repository.FooRepository;
|
import org.baeldung.boot.repository.FooRepository;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class FooComponent {
|
public class FooComponent {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FooRepository fooRepository;
|
private FooRepository fooRepository;
|
||||||
|
|
||||||
private final static Logger logger = org.slf4j.LoggerFactory.getLogger(FooComponent.class);
|
public Foo getFooWithId(Integer id) throws Exception {
|
||||||
|
return fooRepository.findOne(id);
|
||||||
public Foo getFooWithId(Integer id) {
|
|
||||||
|
|
||||||
Foo foo = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
foo = fooRepository.findOne(id);
|
|
||||||
Preconditions.checkNotNull(foo);
|
|
||||||
logger.info("Foo with Id {} found",id);
|
|
||||||
}catch(NullPointerException ex){
|
|
||||||
logger.error("Foo with Id {} was not found",id);
|
|
||||||
throw new FooNotFoundException("The given foo Id was not found");
|
|
||||||
} catch (Exception ex) {
|
|
||||||
logger.error("Error while retrieving Foo with Id {} found",id,ex);
|
|
||||||
throw new CommonException("Error while retrieving foo");
|
|
||||||
}
|
|
||||||
|
|
||||||
return foo;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Foo getFooWithName(String name) {
|
public Foo getFooWithName(String name) {
|
||||||
|
return fooRepository.findByName(name);
|
||||||
Foo foo = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
foo = fooRepository.findByName(name);
|
|
||||||
Preconditions.checkNotNull(foo);
|
|
||||||
logger.info("Foo with name {} found",name);
|
|
||||||
}catch(NullPointerException ex){
|
|
||||||
logger.error("Foo with name {} was not found",name);
|
|
||||||
throw new FooNotFoundException("The given foo name was not found");
|
|
||||||
} catch (Exception ex) {
|
|
||||||
logger.error("Error while retrieving Foo with name {} found",name,ex);
|
|
||||||
throw new CommonException("Error while retrieving foo");
|
|
||||||
}
|
|
||||||
|
|
||||||
return foo;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,19 +1,13 @@
|
|||||||
package org.baeldung.boot.service;
|
package org.baeldung.boot.service;
|
||||||
|
|
||||||
import org.baeldung.boot.components.FooComponent;
|
import org.baeldung.boot.components.FooComponent;
|
||||||
import org.baeldung.boot.exceptions.CommonException;
|
|
||||||
import org.baeldung.boot.exceptions.FooNotFoundException;
|
|
||||||
import org.baeldung.boot.model.Foo;
|
import org.baeldung.boot.model.Foo;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@ -22,31 +16,19 @@ public class FooService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private FooComponent fooComponent;
|
private FooComponent fooComponent;
|
||||||
|
|
||||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
@GetMapping("/{id}")
|
||||||
public ResponseEntity<?> getFooWithId(@PathVariable Integer id) {
|
public ResponseEntity<?> getFooWithId(@PathVariable Integer id) throws Exception {
|
||||||
|
|
||||||
Foo foo = fooComponent.getFooWithId(id);
|
Foo foo = fooComponent.getFooWithId(id);
|
||||||
|
|
||||||
return new ResponseEntity<Foo>(foo, HttpStatus.OK);
|
return new ResponseEntity<>(foo, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
@GetMapping("/")
|
||||||
public ResponseEntity<?> getFooWithName(@RequestParam String name) {
|
public ResponseEntity<?> getFooWithName(@RequestParam String name) throws Exception {
|
||||||
|
|
||||||
Foo foo = fooComponent.getFooWithName(name);
|
Foo foo = fooComponent.getFooWithName(name);
|
||||||
|
|
||||||
return new ResponseEntity<Foo>(foo, HttpStatus.OK);
|
return new ResponseEntity<>(foo, HttpStatus.OK);
|
||||||
}
|
|
||||||
|
|
||||||
@ExceptionHandler(value = FooNotFoundException.class)
|
|
||||||
@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Foo not found")
|
|
||||||
public void handleFooNotFoundException() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExceptionHandler(value = CommonException.class)
|
|
||||||
@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR, reason = "Common exception")
|
|
||||||
public void handleCommonException() {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,13 +1,5 @@
|
|||||||
package org.baeldung.boot;
|
package org.baeldung.boot;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.mockito.Matchers.anyInt;
|
|
||||||
import static org.mockito.Mockito.doReturn;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.baeldung.boot.components.FooComponent;
|
import org.baeldung.boot.components.FooComponent;
|
||||||
import org.baeldung.boot.model.Foo;
|
import org.baeldung.boot.model.Foo;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -22,8 +14,18 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.mockito.Matchers.anyInt;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = DemoApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(
|
||||||
|
classes = DemoApplication.class,
|
||||||
|
webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
public class FooComponentTests {
|
public class FooComponentTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -33,7 +35,7 @@ public class FooComponentTests {
|
|||||||
private FooComponent fooComponent;
|
private FooComponent fooComponent;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() throws Exception {
|
||||||
Foo foo = new Foo();
|
Foo foo = new Foo();
|
||||||
foo.setId(5);
|
foo.setId(5);
|
||||||
foo.setName("MOCKED_FOO");
|
foo.setName("MOCKED_FOO");
|
||||||
@ -45,7 +47,7 @@ public class FooComponentTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInquiryingFooWithId_whenFooComponentIsMocked_thenAssertMockedResult() {
|
public void givenInquiryingFooWithId_whenFooComponentIsMocked_thenAssertMockedResult() {
|
||||||
Map<String, String> pathVariables = new HashMap<String, String>();
|
Map<String, String> pathVariables = new HashMap<>();
|
||||||
pathVariables.put("id", "1");
|
pathVariables.put("id", "1");
|
||||||
ResponseEntity<Foo> fooResponse = testRestTemplate.getForEntity("/{id}", Foo.class, pathVariables);
|
ResponseEntity<Foo> fooResponse = testRestTemplate.getForEntity("/{id}", Foo.class, pathVariables);
|
||||||
|
|
||||||
@ -57,7 +59,7 @@ public class FooComponentTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInquiryingFooWithName_whenFooComponentIsMocked_thenAssertMockedResult() {
|
public void givenInquiryingFooWithName_whenFooComponentIsMocked_thenAssertMockedResult() {
|
||||||
Map<String, String> pathVariables = new HashMap<String, String>();
|
Map<String, String> pathVariables = new HashMap<>();
|
||||||
pathVariables.put("name", "Foo_Name");
|
pathVariables.put("name", "Foo_Name");
|
||||||
ResponseEntity<Foo> fooResponse = testRestTemplate.getForEntity("/?name={name}", Foo.class, pathVariables);
|
ResponseEntity<Foo> fooResponse = testRestTemplate.getForEntity("/?name={name}", Foo.class, pathVariables);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user