小编Zac*_*cus的帖子

Spring Boot 2.1:未从 application-test.yml 加载属性

我试图在我的单元测试执行期间从 application-test.yml 读取一个属性,但相反,正在读取 application-dev.yml 中的属性。我没有 application.yml 文件。感谢帮助。

应用属性.java

@Component
@ConfigurationProperties(prefix="app")
public class AppProperties {

    private String test;    

    public String getTest() {
        return this.test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序-dev.yml

spring:
  profiles: dev
  application:
    name: testApplication
app:
  test: 1
Run Code Online (Sandbox Code Playgroud)

应用程序-test.yml

spring:
  profiles: test
  application:
    name: testApplication
app:
  test: 2
Run Code Online (Sandbox Code Playgroud)

应用服务测试.java

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {AppProperties.class}, initializers= ConfigFileApplicationContextInitializer.class)
@EnableConfigurationProperties
@ActiveProfiles("test")
public class AppServiceTest{

@Autowired
AppProperties appProperties;

@Test
public void test(){    
    appProperties.getTest();  
    //This returns "1" instead of the …
Run Code Online (Sandbox Code Playgroud)

java spring-boot

7
推荐指数
1
解决办法
5067
查看次数

Spring Boot 2.1.1:java.lang.IllegalStateException:运行单元测试时无法检索@EnableAutoConfiguration基础包错误

执行单元测试时将抛出以下错误。请告知我是否遗漏了什么。我正在使用 Spring Boot 2.1.1.RELEASE。谢谢!

java.lang.IllegalStateException:无法检索@EnableAutoConfiguration 基础包


应用程序-test.yml

spring:
  profiles: test
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
    username : xxx
    password : xxx
  jpa:
    hibernate:
      ddl-auto: update
  cache:
    type: simple
Run Code Online (Sandbox Code Playgroud)

应用程序库.java

@Repository
public interface AppRepository extends CrudRepository<App, Integer> {

    App findFirstByAppId(String appId);

}   
Run Code Online (Sandbox Code Playgroud)

AppRepositoryTest.java

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {AppRepository.class})
@EnableConfigurationProperties
@DataJpaTest
@ActiveProfiles("test")
public class AppRepositoryTest {

    @Autowired
    AppRepository appRepository;

    @Before
    public void setUp() throws Exception {
        App app = new App();
        app.setAppId("testId");
        appRepository.save(app);
    }

    @Test
    public void testFindFirstByAppId() {
        assertNotNull(appRepository.findFirstByAppId("testId"));        
    }
}
Run Code Online (Sandbox Code Playgroud)

封装结构 …

unit-testing spring-data-jpa spring-boot

7
推荐指数
2
解决办法
1万
查看次数

标签 统计

spring-boot ×2

java ×1

spring-data-jpa ×1

unit-testing ×1