Spring boot 未能加载ApplicationContext
我是测试新手。我创建了一个测试用例,使用JUNIT Mockito对rest API执行测试。在我的代码中,我已经在方法itemGetByid()上创建了一个测试,但是当我运行该测试时,会收到ApplocationContext错误消息。我不知道我错在哪里Spring boot 未能加载ApplicationContext,spring-boot,spring-mvc,mockito,junit4,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; impor
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> {
}
项目控制器测试类
包com.example.demo.controller;
导入com.example.demo.entities.Item;
导入com.example.demo.entities.User;
导入com.example.demo.service.ItemService;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.mockito.InjectMocks;
导入org.mockito.Mock;
导入org.mockito.mockito;
导入org.mockito.stubing.Answer;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
导入org.springframework.boot.test.context.SpringBootTest;
导入org.springframework.test.context.ContextConfiguration;
导入org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
导入org.springframework.test.context.junit4.SpringRunner;
导入org.springframework.test.web.servlet.MockMvc;
导入java.util.*;
导入静态org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(ItemController.class)
公共类ItemControllerTest{
@自动连线
MockMvc-MockMvc;
@嘲弄
项目服务项目服务;
@注射模拟
项目控制器项目控制器;
@试验
public void itemGetById(){
项目=新项目();
Mockito.when(itemController.getById(10L)),然后返回(item);
Item i=itemController.getById(10L);
资产质量(一,项目);
}
}
项目实体类
包com.example.demo.entities;
导入龙目数据;
导入javax.persistence.*;
@资料
@实体
@表(name=“Item”)
公共类项目{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私有长项目ID;
私人长客户ID;
私有字符串itemName;
私人物品价格;
}
项目控制器
包com.example.demo.controller;
导入com.example.demo.entities.Item;
导入com.example.demo.entities.User;
导入com.example.demo.service.ItemService;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.web.bind.annotation.*;
@RestController
@请求映射(“项”)
公共类ItemController{
私人项目服务项目服务;
@自动连线
公共项目控制器(项目服务项目服务){
this.itemService=itemService;
}
@邮戳(“/post”)
公共项目帖子(@RequestBody-Item){
退货项目服务。邮寄(项目);
}
@GetMapping
公共Iterable getAll(){
return itemService.get();
}
@GetMapping(“/get”)
公共项getById(长id){
returnitemservice.getItem(id);
}
@DeleteMapping(“/delete”)
public void deleteAll(){
itemService.deleteAll();
}
}
项目服务
包com.example.demo.service;
导入com.example.demo.entities.Item;
导入com.example.demo.userepository.ItemRepository;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Service;
导入org.springframework.web.bind.annotation.RequestBody;
@服务
公共类项目服务{
项目库项目库;
@自动连线
公共项目服务(ItemRepository ItemRepository){
this.itemRepository=itemRepository;
}
公共项目帖子(@RequestBody-Item){
返回itemRepository.save(项目);
}
公共Iterable get(){
返回itemRepository.findAll();
}
public void deleteAll(){
itemRepository.deleteAll();
}
公共项getItem(长id){
返回itemRepository.findById(id.get();
}
}
项目存储库
包com.example.demo.userepository;
导入com.example.demo.entities.Item;
导入org.springframework.data.repository.crudepository;
导入org.springframework.stereotype.Repository;
@存储库
公共接口ItemRepository扩展了Crudepository{
}
首先,您使用的是SpringJUnit4ClassRunner。试着模仿一下。
测试中从未创建ItemController实例,因此也需要修复。
下面的行将失败,因为itemController不是模拟的
**Mockito.when(itemController.getById(10L)).thenReturn(item);**
如果itemController使用when语句转换为mock,则测试方法不会验证任何内容,因为测试告诉Mockito在when语句中返回该项。
假设目的是验证ItemController getById方法,Mockito.when语句需要描述对Mock ItemService的调用。您能提供有关您得到的实际异常的信息吗?