dimanche 20 décembre 2015

Spring Unit test on Controller Java

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://ift.tt/GArMu6"
    xmlns:context="http://ift.tt/GArMu7"
    xmlns:xsi="http://ift.tt/ra1lAU" xmlns:mvc="http://ift.tt/1bHqwjR"
    xsi:schemaLocation="
        http://ift.tt/GArMu6     
        http://ift.tt/1jdM0fG
        http://ift.tt/GArMu7 
        http://ift.tt/1jdLYo7
        http://ift.tt/1bHqwjR
        http://ift.tt/1fmimld">

    <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));

    }

}

Aucun commentaire:

Enregistrer un commentaire