Controller Java上的Spring Unit测试

时间:2015-12-20 13:46:42

标签: java spring unit-testing

我有这个BookController和一个名为BookService的服务。我试图测试控制器,但我有一些困难。我在网络中找到了一些示例,但我无法导入when关键字,我无法测试测试是否正确。如果这不是写测试的正确方法,请告诉我如何制作它。

@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 个答案:

答案 0 :(得分:1)

您必须添加Mockito静态导入

import static org.mockito.Mockito.*;

如果静态导入为您提供编译错误,则表示您没有正确设置mockito依赖项:

<dependency>
   <groupId>org.mockito</groupId>
   <artifactId>mockito-all</artifactId>
   <version>${mockito.last_version}</version>
</dependency>