0

I have this BookController and a service called BookService. I am trying to test the controller but i have some dofficulties with that. I found some examples in the network but i can not import the when keyword and i can not test if the test is correct. Please if this is not the correct way to write my test, tell me how to make it.

@Controller
@RequestMapping("books")
public class BookController {

    @Autowired
    BookService bookService;

    @RequestMapping("/all")
    public String getAllbooks(Model model) {
        model.addAttribute("allBooks", bookService.getAllBooks());
        return "books";
    }


    @RequestMapping("{category}")
    public String getProductsByCategory(Model model, @PathVariable("category") String productCategory) {
        model.addAttribute("allBooks", bookService.getBookByCategory(productCategory));
        return "books";
    }


    @RequestMapping("/filter/{ByCriteria}")
    public String getProductsByFilter(@MatrixVariable(pathVar = "ByCriteria") Map<String, List<String>> filterParams,
            Model model) {
        model.addAttribute("allBooks", bookService.getBooksByFilter(filterParams));
        return "books";
    }

    @RequestMapping("/product")
    public String getProductById(@RequestParam("id") String productId, Model model) {
        model.addAttribute("allBooks", bookService.getBookById(productId));
        return "book";
    }

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookRepository bookRepository;

    @Override
    public List<Book> getAllBooks() {
        return bookRepository.getAllBooks();
    }

    @Override
    public Book getBookById(String productID) {
        return bookRepository.getBookById(productID);
    }

    @Override
    public List<Book> getBookByCategory(String category) {
        return bookRepository.getBookByCategory(category);
    }

    @Override
    public Set<Book> getBooksByFilter(Map<String, List<String>> filterParams) {
        return bookRepository.getBooksByFilter(filterParams);
    }

}

 //This is the ApplicationContext.xml for tests
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.book" />
</beans>

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml" })
@WebAppConfiguration
@RequestMapping("books")
public class BookControllerTest {

    @Mock
    private BookService bookService;

    @InjectMocks
    private BookController bookController;

    private MockMvc mockMvc;

    @Before
    public void before() {
        MockitoAnnotations.initMocks(this);
        this.mockMvc = MockMvcBuilders.standaloneSetup(bookController).build();
    }

    @Test
    @RequestMapping("/all")
    public void testGetAllBooks() {

        Book book1 = new Book("4", "Book", "Book", 55);
        Book book2 = new Book("7", "Book2", "Book2", 59);

        when(bookService.getAllBooks()).thenReturn(Arrays.asList(book1, book2));

    }

}

1 Answers1

1

You have to add the Mockito static imports

import static org.mockito.Mockito.*;

If the static import gives you compilation error, that means you don't have the mockito dependency set properly:

<dependency>
   <groupId>org.mockito</groupId>
   <artifactId>mockito-all</artifactId>
   <version>${mockito.last_version}</version>
</dependency>
snovelli
  • 5,804
  • 2
  • 37
  • 50
  • Thank you! Now it works. The thing that i can not understand is why i have to import Mockito static imports manualy ? I have mockito-all dependency in my pom.xml. – NoSuchUserException Dec 20 '15 at 14:18
  • 1
    static import is just to avoid using Mockito.when each time. It's syntactical sugar to make the code more readable – snovelli Dec 20 '15 at 14:33