import org.junit.jupiter.api.Test和import org.springframework.boot.test.context.SpringBootTest报错

在创建新的Spring项目时,遇到使用`@Test`和`@SpringBootTest`报错的问题。错误源于IDEA未下载对应jar包。解决方法包括在pom.xml添加依赖,手动将缺失的jar包下载到Maven本地仓库,以及通过MAVEN中央仓库获取jar。执行Build Project后,问题通常可得到解决。
摘要由CSDN通过智能技术生成

import org.junit.jupiter.api.Test和import org.springframework.boot.test.context.SpringBootTest报错

今天遇见了这个bug,创建新的spring项目,运行报错,看过很多人分享的解决方法,简单来说就是IDEA没有下载对应的jar包,你需要做的就是把对应的jar包下载到你的maven本地仓库

首先,你的pom.xml文件中应该有对应的依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            
这是我的全部代码package com.system.controller; import com.alibaba.fastjson.JSONObject; import org.junit.Assert; import org.junit.Test; import org.junit.jupiter.api.BeforeEach; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class LoginControllerTest { @Autowired private WebApplicationContext context; private MockMvc mockMvc; @BeforeEach public void setUp() { mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); Assert.assertNotNull("mockMvc 对象未正确初始化", mockMvc); } @Test public void testLogin() throws Exception { JSONObject request = new JSONObject(); request.put("username", "user1"); request.put("password", "123456"); mockMvc.perform(MockMvcRequestBuilders.post("/login") .content(request.toJSONString()) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()); } }
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值