Skip to content

Commit 250cb7c

Browse files
authored
Refactor Spring-boot-test examples (eugenp#1328)
* Refactor Spring-boot-test examples * Refactor Spring-boot-test examples
1 parent 656afd6 commit 250cb7c

3 files changed

Lines changed: 24 additions & 77 deletions

File tree

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,21 @@
11
package org.baeldung.boot.components;
22

3-
import org.baeldung.boot.exceptions.CommonException;
4-
import org.baeldung.boot.exceptions.FooNotFoundException;
53
import org.baeldung.boot.model.Foo;
64
import org.baeldung.boot.repository.FooRepository;
7-
import org.slf4j.Logger;
85
import org.springframework.beans.factory.annotation.Autowired;
96
import org.springframework.stereotype.Component;
107

11-
import com.google.common.base.Preconditions;
12-
138
@Component
149
public class FooComponent {
1510

1611
@Autowired
1712
private FooRepository fooRepository;
1813

19-
private final static Logger logger = org.slf4j.LoggerFactory.getLogger(FooComponent.class);
20-
21-
public Foo getFooWithId(Integer id) {
22-
23-
Foo foo = null;
24-
25-
try {
26-
foo = fooRepository.findOne(id);
27-
Preconditions.checkNotNull(foo);
28-
logger.info("Foo with Id {} found",id);
29-
}catch(NullPointerException ex){
30-
logger.error("Foo with Id {} was not found",id);
31-
throw new FooNotFoundException("The given foo Id was not found");
32-
} catch (Exception ex) {
33-
logger.error("Error while retrieving Foo with Id {} found",id,ex);
34-
throw new CommonException("Error while retrieving foo");
35-
}
36-
37-
return foo;
14+
public Foo getFooWithId(Integer id) throws Exception {
15+
return fooRepository.findOne(id);
3816
}
3917

4018
public Foo getFooWithName(String name) {
41-
42-
Foo foo = null;
43-
44-
try {
45-
foo = fooRepository.findByName(name);
46-
Preconditions.checkNotNull(foo);
47-
logger.info("Foo with name {} found",name);
48-
}catch(NullPointerException ex){
49-
logger.error("Foo with name {} was not found",name);
50-
throw new FooNotFoundException("The given foo name was not found");
51-
} catch (Exception ex) {
52-
logger.error("Error while retrieving Foo with name {} found",name,ex);
53-
throw new CommonException("Error while retrieving foo");
54-
}
55-
56-
return foo;
19+
return fooRepository.findByName(name);
5720
}
5821
}
Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
package org.baeldung.boot.service;
22

33
import org.baeldung.boot.components.FooComponent;
4-
import org.baeldung.boot.exceptions.CommonException;
5-
import org.baeldung.boot.exceptions.FooNotFoundException;
64
import org.baeldung.boot.model.Foo;
75
import org.springframework.beans.factory.annotation.Autowired;
86
import org.springframework.http.HttpStatus;
9-
import org.springframework.http.MediaType;
107
import org.springframework.http.ResponseEntity;
11-
import org.springframework.web.bind.annotation.ExceptionHandler;
8+
import org.springframework.web.bind.annotation.GetMapping;
129
import org.springframework.web.bind.annotation.PathVariable;
13-
import org.springframework.web.bind.annotation.RequestMapping;
14-
import org.springframework.web.bind.annotation.RequestMethod;
1510
import org.springframework.web.bind.annotation.RequestParam;
16-
import org.springframework.web.bind.annotation.ResponseStatus;
1711
import org.springframework.web.bind.annotation.RestController;
1812

1913
@RestController
@@ -22,31 +16,19 @@ public class FooService {
2216
@Autowired
2317
private FooComponent fooComponent;
2418

25-
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
26-
public ResponseEntity<?> getFooWithId(@PathVariable Integer id) {
19+
@GetMapping("/{id}")
20+
public ResponseEntity<?> getFooWithId(@PathVariable Integer id) throws Exception {
2721

2822
Foo foo = fooComponent.getFooWithId(id);
2923

30-
return new ResponseEntity<Foo>(foo, HttpStatus.OK);
24+
return new ResponseEntity<>(foo, HttpStatus.OK);
3125
}
3226

33-
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
34-
public ResponseEntity<?> getFooWithName(@RequestParam String name) {
27+
@GetMapping("/")
28+
public ResponseEntity<?> getFooWithName(@RequestParam String name) throws Exception {
3529

3630
Foo foo = fooComponent.getFooWithName(name);
3731

38-
return new ResponseEntity<Foo>(foo, HttpStatus.OK);
39-
}
40-
41-
@ExceptionHandler(value = FooNotFoundException.class)
42-
@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Foo not found")
43-
public void handleFooNotFoundException() {
44-
45-
}
46-
47-
@ExceptionHandler(value = CommonException.class)
48-
@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR, reason = "Common exception")
49-
public void handleCommonException() {
50-
32+
return new ResponseEntity<>(foo, HttpStatus.OK);
5133
}
5234
}

‎spring-boot/src/test/java/org/baeldung/boot/FooComponentTests.java‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
package org.baeldung.boot;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertNotNull;
5-
import static org.mockito.Matchers.anyInt;
6-
import static org.mockito.Mockito.doReturn;
7-
8-
import java.util.HashMap;
9-
import java.util.Map;
10-
113
import org.baeldung.boot.components.FooComponent;
124
import org.baeldung.boot.model.Foo;
135
import org.junit.Before;
@@ -22,8 +14,18 @@
2214
import org.springframework.http.ResponseEntity;
2315
import org.springframework.test.context.junit4.SpringRunner;
2416

17+
import java.util.HashMap;
18+
import java.util.Map;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.mockito.Matchers.anyInt;
23+
import static org.mockito.Mockito.doReturn;
24+
2525
@RunWith(SpringRunner.class)
26-
@SpringBootTest(classes = DemoApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
26+
@SpringBootTest(
27+
classes = DemoApplication.class,
28+
webEnvironment = WebEnvironment.RANDOM_PORT)
2729
public class FooComponentTests {
2830

2931
@Autowired
@@ -33,7 +35,7 @@ public class FooComponentTests {
3335
private FooComponent fooComponent;
3436

3537
@Before
36-
public void init() {
38+
public void init() throws Exception {
3739
Foo foo = new Foo();
3840
foo.setId(5);
3941
foo.setName("MOCKED_FOO");
@@ -45,7 +47,7 @@ public void init() {
4547

4648
@Test
4749
public void givenInquiryingFooWithId_whenFooComponentIsMocked_thenAssertMockedResult() {
48-
Map<String, String> pathVariables = new HashMap<String, String>();
50+
Map<String, String> pathVariables = new HashMap<>();
4951
pathVariables.put("id", "1");
5052
ResponseEntity<Foo> fooResponse = testRestTemplate.getForEntity("/{id}", Foo.class, pathVariables);
5153

@@ -57,7 +59,7 @@ public void givenInquiryingFooWithId_whenFooComponentIsMocked_thenAssertMockedRe
5759

5860
@Test
5961
public void givenInquiryingFooWithName_whenFooComponentIsMocked_thenAssertMockedResult() {
60-
Map<String, String> pathVariables = new HashMap<String, String>();
62+
Map<String, String> pathVariables = new HashMap<>();
6163
pathVariables.put("name", "Foo_Name");
6264
ResponseEntity<Foo> fooResponse = testRestTemplate.getForEntity("/?name={name}", Foo.class, pathVariables);
6365

0 commit comments

Comments
 (0)