解决java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use错误

今天在学习springboot结合redis实现二级缓存时,项目依赖引入完成后想要测试redis是否可以连接成功,结果在测试时出现如下错误日志

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:73)
	at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:243)
	at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:155)
	at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:395)
	at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:312)
	at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:265)
	at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:108)
	at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:99)
	at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:139)
	at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:124)

测试用例代码

/**
 * redis连接测试
 *
 * @author 段誉
 * @create 2019-04-11 9:27
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RediscacheApplicationTest {
  @Autowired
  private StringRedisTemplate stringRedisTemplate;

  @Test
  public void contextLoads() {
    stringRedisTemplate.opsForValue().set("k1", "v1");
    System.out.println(stringRedisTemplate.opsForValue().get("k1"));
  }
}

原因

是因为我在搭建好springboot项目后没有创建Application.java项目启动类导致的

解决办法

创建完Application.java后,再重新运行测试类正常运行
在这里插入图片描述

参考博客

https://blog.csdn.net/weixin_39220472/article/details/80206449

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值