ApplicationContext加载失败

时间:2019-02-09 05:56:09

标签: spring-boot spring-mvc mockito junit4

我是测试新手。我创建了一个测试用例,以使用JUNIT Mockito对rest API进行测试。在我的代码中,我已经对方法itemGetByid()创建了一个测试,但是当我运行测试时,我得到了ApplocationContext错误消息。我不知道我要去哪里错了。

Item Controller Test class
    package com.example.demo.controller;

    import com.example.demo.entities.Item;
    import com.example.demo.entities.User;
    import com.example.demo.service.ItemService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.InjectMocks;
    import org.mockito.Mock;
    import org.mockito.Mockito;
    import org.mockito.stubbing.Answer;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import java.util.*;
    import static org.junit.Assert.*;

    @RunWith(SpringJUnit4ClassRunner.class)

    @WebMvcTest(ItemController.class)
    public class ItemControllerTest {

        @Autowired
        MockMvc mockmvc;
        @Mock
        ItemService itemService;
        @InjectMocks
        ItemController itemController;

        @Test
        public void itemGetById() {

            Item item = new Item();
            Mockito.when(itemController.getById(10L)).thenReturn(item);
            Item i = itemController.getById(10L);
            assertEquals(i, item);

        }
    }

    Item entity class
    package com.example.demo.entities;

    import lombok.Data;
    import javax.persistence.*;

    @Data
    @Entity
    @Table(name = "Item")
    public class Item {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long itemId;
        private Long customerId;
        private String itemName;
        private int itemPrice;

    }
    Item controller
    package com.example.demo.controller;

    import com.example.demo.entities.Item;
    import com.example.demo.entities.User;
    import com.example.demo.service.ItemService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    @RestController
    @RequestMapping("item")
    public class ItemController {

        private ItemService itemService;

        @Autowired
        public ItemController(ItemService itemService) {
            this.itemService = itemService;
        }

        @PostMapping("/post")
        public Item post(@RequestBody Item item) {
            return itemService.post(item);
        }

        @GetMapping
        public Iterable<Item> getAll() {
            return itemService.get();
        }

        @GetMapping("/get")
        public Item getById(Long id) {
            return itemService.getItem(id);
        }

        @DeleteMapping("/delete")
        public void deleteAll() {
            itemService.deleteAll();
        }

    }

    Item Service
    package com.example.demo.service;

    import com.example.demo.entities.Item;
    import com.example.demo.userepository.ItemRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.bind.annotation.RequestBody;

    @Service
    public class ItemService {
        ItemRepository itemRepository;

        @Autowired
        public ItemService(ItemRepository itemRepository) {

            this.itemRepository = itemRepository;
        }

        public Item post(@RequestBody Item item) {
            return itemRepository.save(item);
        }

        public Iterable<Item> get() {
            return itemRepository.findAll();
        }

        public void deleteAll() {
            itemRepository.deleteAll();
        }

        public Item getItem(Long id) {
            return itemRepository.findById(id).get();
        }

    }

    Item Repository
    package com.example.demo.userepository;

    import com.example.demo.entities.Item;
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.stereotype.Repository;

    @Repository
    public interface ItemRepository extends CrudRepository<Item, Long> {
    } 

1 个答案:

答案 0 :(得分:0)

对于初学者,您正在使用SpringJUnit4ClassRunner。试试MockitoJunitRunner。 在测试中不会创建ItemController实例,因此也需要对其进行修复。 下面的行将失败,因为itemController不是一个模拟。

**Mockito.when(itemController.getById(10L)).thenReturn(item);**

如果使用when语句将itemController转换为模拟,则test方法不会验证任何内容,因为测试告诉Mockito返回when语句中的项目。 假设目的是验证ItemController的getById方法,则Mockito.when语句需要描述对Mock ItemService的调用。