diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java index ad634d7c1b..78a6e06e7a 100644 --- a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java +++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java @@ -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 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 list = tutService.listTutorials(); + model.addAttribute("tutorials", list); + return "index"; + } + + public TutorialsService getTutService() { + return tutService; + } } \ No newline at end of file diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/domain/Tutorial.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/domain/Tutorial.java index ced7482758..bc0118790a 100644 --- a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/domain/Tutorial.java +++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/domain/Tutorial.java @@ -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; + } } diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/service/TutorialsService.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/service/TutorialsService.java index 59c2f04b6c..4b6c5de427 100644 --- a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/service/TutorialsService.java +++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/service/TutorialsService.java @@ -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 listTutorials() { - List list = new ArrayList(); - - list.add(new Tutorial(1, "Guava", "Introduction to Guava","GuavaAuthor")); - list.add(new Tutorial(2, "Android", "Introduction to Android","AndroidAuthor")); - return list; - } + + public List listTutorials() { + return Arrays.asList( + new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"), + new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor") + ); + } } diff --git a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java index bcd42ae47d..332c4d4c33 100644 --- a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java +++ b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java @@ -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 tutorialListAttribute = (List) model.asMap().get("tutorials"); @@ -48,6 +38,7 @@ public class NavigationControllerTest { } - - + private static List createTutorialList() { + return Arrays.asList(new Tutorial(1, "TestAuthor", "Test Title", "Test Description")); + } } diff --git a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/TutorialDataFactory.java b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/TutorialDataFactory.java deleted file mode 100644 index 4e76d0ea5b..0000000000 --- a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/TutorialDataFactory.java +++ /dev/null @@ -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 createTutorialList() { - final List tutorialList = new ArrayList(); - tutorialList.add(createByDefault()); - return tutorialList; - } - -}