Sprintboot的一个测试用例

最近做测试代码,编译总是不通过报出下面的错误

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
    at org.springframework.util.Assert.state(Assert.java:70)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:202)

查了一下百度后来发现是测试代码与源代码 的package 不一样导致。
后来将他们的包名改为一样的就可以了
例如:

test
--package com.xxx.yyy.controller;
src
--package com.xxx.yyy.controller;

测试代码如下

package com.xxx.yyy.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;

import com.alibaba.fastjson.JSON;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class TestXXXController {
	@Autowired
	private WebApplicationContext wac;

	private MockMvc mvc;
	private MockHttpSession session;
	
	@LocalServerPort
	private int port;
	
	private String host = "http://localhost:";
	
	private String path = "xxx";
	
	@Autowired
	private TestRestTemplate restTemplate;

	@Test
	public void testList() throws UnsupportedEncodingException{
		Map<String, Object> param =new HashMap<String, Object>();
		param.put("id", 19);
		param.put("menuId", "1");
		StringBuffer sb = getParameterString(param);
		
		String json = this.restTemplate.getForObject("http://localhost:" + port + "/xxx/ccc/list.json?"+sb.toString(),
				String.class);
		System.out.println(json);
	}
}
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在SpringBoot项目中,如果我们想跳过测试用例的执行,有几种方式可以实现。一种方式是在mvn install命令中使用-skipTests参数来跳过测试单元的执行。这样,在编译和打包的过程中,测试代码将不会被执行。另一种方式是在pom.xml文件中配置spring-boot-maven-plugin插件的skipTests参数为true,这样在执行mvn install命令时,测试代码也会被跳过。除此之外,还可以使用-DskipTests参数来跳过测试单元的执行。例如,运行mvn install -DskipTests命令可以跳过测试代码的执行。 另外,如果你只想跳过某个特定的测试用例,你可以在运行测试命令时使用-Dtest参数,并指定要跳过的测试用例的名称。例如,运行mvn test -Dtest=HelloControllerTest#hello*命令可以跳过HelloControllerTest类中以hello开头的所有测试用例的执行。 总之,为了保证代码的质量,我们在开发过程中应该编写充分的单元测试用例。这样可以帮助我们及时发现和解决潜在的问题,提高代码的可靠性和可维护性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot 打包jar包时,跳过单元测试](https://blog.csdn.net/dajiangqingzhou/article/details/88546916)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [10分钟学会Springboot项目单元测试](https://blog.csdn.net/m0_67695717/article/details/124666276)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值