Refactor mvc-velocity examples

This commit is contained in:
Grzegorz Piwowarek 2016-07-17 22:21:16 +03:00
parent 1f5f1c6a48
commit e883cf63a1
5 changed files with 59 additions and 155 deletions

View File

@ -1,36 +1,33 @@
package com.baeldung.mvc.velocity.controller;
import java.util.List;
import com.baeldung.mvc.velocity.domain.Tutorial;
import com.baeldung.mvc.velocity.service.TutorialsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.baeldung.mvc.velocity.domain.Tutorial;
import com.baeldung.mvc.velocity.service.TutorialsService;
import java.util.List;
@Controller
public class MainController {
@Autowired
private TutorialsService tutService;
@RequestMapping(value = { "/" }, method = RequestMethod.GET)
public String listTutorialsPage(Model model) {
List<Tutorial> list = tutService.listTutorials();
model.addAttribute("tutorials", list);
return "index";
}
public TutorialsService getTutService() {
return tutService;
}
private final TutorialsService tutService;
public void setTutService(TutorialsService tutService) {
this.tutService = tutService;
}
@Autowired
public MainController(TutorialsService tutService) {
this.tutService = tutService;
}
@RequestMapping(method = RequestMethod.GET)
public String listTutorialsPage(Model model) {
List<Tutorial> list = tutService.listTutorials();
model.addAttribute("tutorials", list);
return "index";
}
public TutorialsService getTutService() {
return tutService;
}
}

View File

@ -2,53 +2,15 @@ package com.baeldung.mvc.velocity.domain;
public class Tutorial {
private Integer tutId;
private String title;
private String description;
private String author;
public Tutorial() {
super();
}
public Tutorial(Integer tutId, String title, String description, String author) {
super();
this.tutId = tutId;
this.title = title;
this.description = description;
this.author = author;
}
public Integer getTutId() {
return tutId;
}
public void setTutId(Integer tutId) {
this.tutId = tutId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
private final Integer tutId;
private final String title;
private final String description;
private final String author;
public Tutorial(Integer tutId, String title, String description, String author) {
this.tutId = tutId;
this.title = title;
this.description = description;
this.author = author;
}
}

View File

@ -1,20 +1,18 @@
package com.baeldung.mvc.velocity.service;
import java.util.ArrayList;
import java.util.List;
import com.baeldung.mvc.velocity.domain.Tutorial;
import org.springframework.stereotype.Service;
import com.baeldung.mvc.velocity.domain.Tutorial;
import java.util.Arrays;
import java.util.List;
@Service
public class TutorialsService {
public List<Tutorial> listTutorials() {
List<Tutorial> list = new ArrayList<Tutorial>();
list.add(new Tutorial(1, "Guava", "Introduction to Guava","GuavaAuthor"));
list.add(new Tutorial(2, "Android", "Introduction to Android","AndroidAuthor"));
return list;
}
public List<Tutorial> listTutorials() {
return Arrays.asList(
new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"),
new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor")
);
}
}

View File

@ -1,45 +1,35 @@
package com.baeldung.mvc.velocity.test;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import com.baeldung.mvc.velocity.controller.MainController;
import com.baeldung.mvc.velocity.domain.Tutorial;
import com.baeldung.mvc.velocity.service.TutorialsService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(MockitoJUnitRunner.class)
public class NavigationControllerTest {
private MainController mainController;
private final MainController mainController = new MainController(Mockito.mock(TutorialsService.class));
private TutorialsService tutorialsService;
private Model model;
@Before
public final void setUp() throws Exception {
model = new ExtendedModelMap();
mainController = Mockito.spy(new MainController());
tutorialsService = Mockito.mock(TutorialsService.class);
mainController.setTutService(tutorialsService);
}
private final Model model = new ExtendedModelMap();
@Test
public final void shouldGoToTutorialListView() {
Mockito.when(tutorialsService.listTutorials()).thenReturn(TutorialDataFactory.createTutorialList());
Mockito.when(mainController.getTutService().listTutorials())
.thenReturn(createTutorialList());
final String view = mainController.listTutorialsPage(model);
final List<Tutorial> tutorialListAttribute = (List<Tutorial>) model.asMap().get("tutorials");
@ -48,6 +38,7 @@ public class NavigationControllerTest {
}
private static List<Tutorial> createTutorialList() {
return Arrays.asList(new Tutorial(1, "TestAuthor", "Test Title", "Test Description"));
}
}

View File

@ -1,44 +0,0 @@
package com.baeldung.mvc.velocity.test;
import java.util.ArrayList;
import java.util.List;
import com.baeldung.mvc.velocity.domain.Tutorial;
public final class TutorialDataFactory {
public static final Integer TEST_TUTORIAL_ID = 1;
public static final String TEST_TUTORIAL_AUTHOR = "TestAuthor";
public static final String TEST_TUTORIAL_TITLE = "Test Title";
public static final String TEST_TUTORIAL_DESCRIPTION = "Test Description";
private TutorialDataFactory() {
}
public static Tutorial createByDefault() {
final Tutorial tutorial = new Tutorial();
tutorial.setTutId(TEST_TUTORIAL_ID);
tutorial.setAuthor(TEST_TUTORIAL_AUTHOR);
tutorial.setTitle(TEST_TUTORIAL_TITLE);
tutorial.setDescription(TEST_TUTORIAL_DESCRIPTION);
return tutorial;
}
public static Tutorial create(final Integer id , final String title) {
final Tutorial tutorial = createByDefault();
tutorial.setTutId(id);
tutorial.setTitle(title);
return tutorial;
}
public static List<Tutorial> createTutorialList() {
final List<Tutorial> tutorialList = new ArrayList<Tutorial>();
tutorialList.add(createByDefault());
return tutorialList;
}
}