ApplicationContext 加载失败

ApplicationContext failed to load

我是测试新手。我创建了一个测试用例来使用 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> {
    } 

首先,您使用的是 SpringJUnit4ClassRunner。试试 MockitoJunitRunner。 ItemController 实例从未在测试中创建,因此也需要修复。 下面的行将失败,因为 itemController 不是模拟的。

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

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