Skip to content

Commit e883cf6

Browse files
author
Grzegorz Piwowarek
committed
Refactor mvc-velocity examples
1 parent 1f5f1c6 commit e883cf6

5 files changed

Lines changed: 59 additions & 155 deletions

File tree

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,33 @@
11
package com.baeldung.mvc.velocity.controller;
22

3-
import java.util.List;
4-
3+
import com.baeldung.mvc.velocity.domain.Tutorial;
4+
import com.baeldung.mvc.velocity.service.TutorialsService;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.stereotype.Controller;
77
import org.springframework.ui.Model;
88
import org.springframework.web.bind.annotation.RequestMapping;
99
import org.springframework.web.bind.annotation.RequestMethod;
1010

11-
import com.baeldung.mvc.velocity.domain.Tutorial;
12-
import com.baeldung.mvc.velocity.service.TutorialsService;
11+
import java.util.List;
1312

1413
@Controller
1514
public class MainController {
16-
17-
@Autowired
18-
private TutorialsService tutService;
19-
20-
@RequestMapping(value = { "/" }, method = RequestMethod.GET)
21-
public String listTutorialsPage(Model model) {
22-
List<Tutorial> list = tutService.listTutorials();
23-
model.addAttribute("tutorials", list);
24-
return "index";
25-
}
2615

27-
public TutorialsService getTutService() {
28-
return tutService;
29-
}
16+
private final TutorialsService tutService;
17+
18+
@Autowired
19+
public MainController(TutorialsService tutService) {
20+
this.tutService = tutService;
21+
}
22+
23+
@RequestMapping(method = RequestMethod.GET)
24+
public String listTutorialsPage(Model model) {
25+
List<Tutorial> list = tutService.listTutorials();
26+
model.addAttribute("tutorials", list);
27+
return "index";
28+
}
3029

31-
public void setTutService(TutorialsService tutService) {
32-
this.tutService = tutService;
33-
}
34-
35-
30+
public TutorialsService getTutService() {
31+
return tutService;
32+
}
3633
}

‎spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/domain/Tutorial.java‎

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,15 @@
22

33
public class Tutorial {
44

5-
private Integer tutId;
6-
private String title;
7-
private String description;
8-
private String author;
9-
10-
public Tutorial() {
11-
super();
12-
}
13-
14-
public Tutorial(Integer tutId, String title, String description, String author) {
15-
super();
16-
this.tutId = tutId;
17-
this.title = title;
18-
this.description = description;
19-
this.author = author;
20-
}
21-
22-
public Integer getTutId() {
23-
return tutId;
24-
}
25-
26-
public void setTutId(Integer tutId) {
27-
this.tutId = tutId;
28-
}
29-
30-
public String getTitle() {
31-
return title;
32-
}
33-
34-
public void setTitle(String title) {
35-
this.title = title;
36-
}
37-
38-
public String getDescription() {
39-
return description;
40-
}
41-
42-
public void setDescription(String description) {
43-
this.description = description;
44-
}
45-
46-
public String getAuthor() {
47-
return author;
48-
}
49-
50-
public void setAuthor(String author) {
51-
this.author = author;
52-
}
53-
5+
private final Integer tutId;
6+
private final String title;
7+
private final String description;
8+
private final String author;
9+
10+
public Tutorial(Integer tutId, String title, String description, String author) {
11+
this.tutId = tutId;
12+
this.title = title;
13+
this.description = description;
14+
this.author = author;
15+
}
5416
}
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
package com.baeldung.mvc.velocity.service;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
3+
import com.baeldung.mvc.velocity.domain.Tutorial;
64
import org.springframework.stereotype.Service;
75

8-
import com.baeldung.mvc.velocity.domain.Tutorial;
6+
import java.util.Arrays;
7+
import java.util.List;
98

109
@Service
1110
public class TutorialsService {
12-
13-
public List<Tutorial> listTutorials() {
14-
List<Tutorial> list = new ArrayList<Tutorial>();
15-
16-
list.add(new Tutorial(1, "Guava", "Introduction to Guava","GuavaAuthor"));
17-
list.add(new Tutorial(2, "Android", "Introduction to Android","AndroidAuthor"));
18-
return list;
19-
}
11+
12+
public List<Tutorial> listTutorials() {
13+
return Arrays.asList(
14+
new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"),
15+
new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor")
16+
);
17+
}
2018
}
Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,35 @@
11
package com.baeldung.mvc.velocity.test;
22

33

4-
import static org.junit.Assert.*;
5-
6-
import java.util.List;
7-
8-
import org.junit.Before;
4+
import com.baeldung.mvc.velocity.controller.MainController;
5+
import com.baeldung.mvc.velocity.domain.Tutorial;
6+
import com.baeldung.mvc.velocity.service.TutorialsService;
97
import org.junit.Test;
108
import org.junit.runner.RunWith;
11-
import org.springframework.ui.ExtendedModelMap;
12-
import org.springframework.ui.Model;
139
import org.mockito.Mockito;
1410
import org.mockito.runners.MockitoJUnitRunner;
15-
import com.baeldung.mvc.velocity.controller.MainController;
16-
import com.baeldung.mvc.velocity.domain.Tutorial;
17-
import com.baeldung.mvc.velocity.service.TutorialsService;
11+
import org.springframework.ui.ExtendedModelMap;
12+
import org.springframework.ui.Model;
13+
14+
import java.util.Arrays;
15+
import java.util.List;
16+
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertNotNull;
1819

1920

2021
@RunWith(MockitoJUnitRunner.class)
2122
public class NavigationControllerTest {
2223

23-
private MainController mainController;
24-
25-
private TutorialsService tutorialsService;
26-
27-
28-
private Model model;
24+
private final MainController mainController = new MainController(Mockito.mock(TutorialsService.class));
2925

30-
@Before
31-
public final void setUp() throws Exception {
32-
model = new ExtendedModelMap();
33-
mainController = Mockito.spy(new MainController());
34-
tutorialsService = Mockito.mock(TutorialsService.class);
35-
36-
mainController.setTutService(tutorialsService);
37-
38-
}
26+
private final Model model = new ExtendedModelMap();
3927

4028
@Test
4129
public final void shouldGoToTutorialListView() {
42-
Mockito.when(tutorialsService.listTutorials()).thenReturn(TutorialDataFactory.createTutorialList());
30+
Mockito.when(mainController.getTutService().listTutorials())
31+
.thenReturn(createTutorialList());
32+
4333
final String view = mainController.listTutorialsPage(model);
4434
final List<Tutorial> tutorialListAttribute = (List<Tutorial>) model.asMap().get("tutorials");
4535

@@ -48,6 +38,7 @@ public final void shouldGoToTutorialListView() {
4838
}
4939

5040

51-
52-
41+
private static List<Tutorial> createTutorialList() {
42+
return Arrays.asList(new Tutorial(1, "TestAuthor", "Test Title", "Test Description"));
43+
}
5344
}

‎spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/TutorialDataFactory.java‎

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)