Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(

23 篇文章 0 订阅
2 篇文章 0 订阅

  SpringBoot 使用 jasypt 加密数据库连接用户名和密码时(具体用法详见 https://blog.csdn.net/piaoranyuji/article/details/91599116),生成加密后的用户名和密码的单元测试类如下所示。

package up.sm.test;

import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class TestEncryDb {

    @Autowired
    private StringEncryptor encryptor;

    @Test
    public void encry() {
        // 加密数据库用户名:root
        String username = encryptor.encrypt("root");
        System.out.println(username);

        // 加密数据库密码:test
        String password = encryptor.encrypt("test");
        System.out.println(password);
    }
}

  启动单元测试之后,报错:
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test,具体日志如下所示。

09:16:06.488 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class up.sm.test.TestEncryDb]
09:16:06.501 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
09:16:06.526 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
09:16:06.584 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [up.sm.test.TestEncryDb] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
09:16:06.617 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [up.sm.test.TestEncryDb], using SpringBootContextLoader
09:16:06.627 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [up.sm.test.TestEncryDb]: class path resource [up/sm/test/TestEncryDb-context.xml] does not exist
09:16:06.628 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [up.sm.test.TestEncryDb]: class path resource [up/sm/test/TestEncryDbContext.groovy] does not exist
09:16:06.628 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [up.sm.test.TestEncryDb]: no resource found for suffixes {-context.xml, Context.groovy}.
09:16:06.630 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [up.sm.test.TestEncryDb]: TestEncryDb does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
09:16:06.755 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [up.sm.test.TestEncryDb]
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)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:151)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:142)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
	at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
	at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


Process finished with exit code -1

  产生错误的原因可能有以下 2 种:

  1. SpringBoot 项目没有写启动类;
  2. 项目中写了启动类,但是启动类所在的包和单元测试的包不在同一级根目录下。

  本次测试时,项目启动类在 up.sm.svc 包下,测试类在 up.sm.test 包下,所以报错。

  解决方法有几种:

  1. 将测试类和项目启动类放在同一个包目录下,即将测试类的 package 修改为:package up.sm.svc;
  2. 如异常所示,需要引入 SpringBoot 项目启动类,同时在注解上加上项目启动类,测试类改动具体如下所示。其中,up.sm.svc.SvcApplication 为项目启动类。
import up.sm.svc.SvcApplication;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SvcApplication.class)

  按以上 2 种方法中的任意一种,均能解决问题。

  博客参考:

  1. https://blog.csdn.net/qq_25406669/article/details/87966497
  2. https://blog.csdn.net/csdn_am/article/details/79757097?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.add_param_isCf&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.add_param_isCf
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值