GVKun编程网logo

如何在JUnit 5下进行Spring Boot Test来解决在YAML文件上外部化的缩进/分层属性

14

如果您想了解如何在JUnit5下进行SpringBootTest来解决在YAML文件上外部化的缩进/分层属性的知识,那么本篇文章将是您的不二之选。同时我们将深入剖析1.springBoot_配置_ya

如果您想了解如何在JUnit 5下进行Spring Boot Test来解决在YAML文件上外部化的缩进/分层属性的知识,那么本篇文章将是您的不二之选。同时我们将深入剖析1.springBoot_配置_yaml文件值获取、Junit 在 spring 中报错 No tests foundmatching 是 Junit 和 spring 版本冲突导致的、org.springframework.test.context.junit4.SpringJUnit4ClassRunner的实例源码、ruby-on-rails – 如何在YAML文件中包含YAML文件?的各个方面,并给出实际的案例分析,希望能帮助到您!

本文目录一览:

如何在JUnit 5下进行Spring Boot Test来解决在YAML文件上外部化的缩进/分层属性

如何在JUnit 5下进行Spring Boot Test来解决在YAML文件上外部化的缩进/分层属性

如何解决如何在JUnit 5下进行Spring Boot Test来解决在YAML文件上外部化的缩进/分层属性?

如何使Spring Boot Test下的JUnit 5解决在YAML文件上外部化的缩进/分层属性?

我想编写测试来验证一些依赖于Environment.getProperty(String key)的逻辑:

@ExtendWith(SpringExtension.class)
class PropertiesReolution_SO_IT {

    @nested
    @TestPropertySource(locations = "classpath:application-test.yml")
    public class ViaYamlFile {

        @Autowired
        private Environment env;

        @Test
        void testGetDottedHierarchicalproperty() throws Exception {
            final String key = "dotted.hierarchical.property";
            assertNotNull(this.env.getProperty(key));
            assertEquals("application-test.yml",this.env.getProperty(key));
        }
    }

}

dotted.hierarchical.property属性是在application-test.yml YAML文件上定义的,如下所示:

dotted:
  hierarchical:
    property: application-test.yml

测试用例失败,其属性评估为null。我被锁定为spring-boot 1.5.8.RELEASE,所以我正在使用org.springframework.test:spring-test-junit5。我创建了Gist with the full example。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

1.springBoot_配置_yaml文件值获取

1.springBoot_配置_yaml文件值获取

前言: 此文章为对springBoot源码学习的一个记录,参考视频

1.简单的创建一个springBoot的项目

创建项目的具体步骤,自行搜索。

2.创建一个实体类

package com.lara.bean;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lara
 * @date 2020/3/1 12:50
 */
@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;
}

上述代码使用到了

  • @Data注解,这是lombok的一个注解,使用此注解会自动为类的属性生成setter、getter、equals、hashCode、toString等方法。 在idea中使用lombok需要安装一下lombok的插件

同时在项目中添加如下依赖:

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
        </dependency>
  • @ConfigurationProperties:将配置文件中相关属性的值映射到这个组件中,一般为了指明使用那些属性,会指明prifex的值。此注解一般搭配使用@EnableAutoConfiguration注解。意思就是使@ConfigurationProperties注解生效
  • @Component 因为@ConfigurationProperties是容器中的功能,因此需要将person组件添加到容器中

3.在启动类中添加@EnableAutoConfiguration

package com.lara;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author lara
 * @date 2020/2/22 16:40
 */

@SpringBootApplication
@EnableAutoConfiguration
public class HelloWorldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}

4.yaml配置文件添加属性的值

5.测试类测试

import com.lara.HelloWorldApplication;
import com.lara.bean.Person;
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.SpringRunner;

/**
 * @author lara
 * @date 2020/3/1 12:55
 */

@RunWith(SpringRunner.class)
@SpringBootTest(classes = HelloWorldApplication.class)
public class HelloWorldApplicationTest {

    @Autowired
    Person person;

    @Test
    public void contextLoads(){
        System.out.println(person);
    }
}

6.总结

  1. 使用yaml文件的值给一个对象赋值,需要在对象上使用@ConfigurationProperties
  2. 此对象必须已经注入到容器中
  3. 要使@ConfigurationProperties注解生效,还必须在启动类上搭配使用@@EnableAutoConfiguration

Junit 在 spring 中报错 No tests foundmatching 是 Junit 和 spring 版本冲突导致的

Junit 在 spring 中报错 No tests foundmatching 是 Junit 和 spring 版本冲突导致的

java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=testDemo], {ExactMatcher:fD

 

上面是报错的信息,说是没有找到匹配的测试的名字,然而我的代码是酱紫的

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTestDemo {
@Resource(name = "userDaoProxy")
private UserDao userDao;

@Test
public void testDemo() {
userDao.save();
userDao.update();
userDao.delete();
userDao.find();
}

}

完全看不出来哪里有问题,我还改了 testDemo 为 Demo,demoTest 等都不行,最后发现可能是 pom 中的 junit 版本和 spring 版本冲突导致的,我使用的 spring 是 4.3.7,junit 是 4.1

后来改为 junit 是 4.2 的就 ok 了,这些版本问题真的是永远止境【滑稽】

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

org.springframework.test.context.junit4.SpringJUnit4ClassRunner的实例源码

org.springframework.test.context.junit4.SpringJUnit4ClassRunner的实例源码

项目:spring-web-api-test-stubber    文件:SpringBootRestControllerTesterStubGenerator.java   
private typespec buildtypespec(List<MethodSpec> methodSpecs,List<FieldSpec> fieldSpecs,MethodSpec setUp) {

        return typespec.classBuilder(this.restControllerModel.getSimpleClassName())
                .addAnnotation(Transactional.class)
                .addAnnotation(
                        AnnotationSpec.builder(RunWith.class)
                                      .addMember("value","$T.class",SpringJUnit4ClassRunner.class)
                                      .build()
                )
                .addAnnotation(
                        AnnotationSpec.builder(ComponentScan.class)
                                      .addMember("basePackages","{$S,$S}","YOUR_DTOs_PACKAGE","YOUR_SERVICEs_PACKAGE")
                                      .build()
                )
                .addAnnotation(SpringBoottest.class)
                .addModifiers(Modifier.PUBLIC)
                .addFields(fieldSpecs)
                .addMethod(setUp)
                .addMethods(methodSpecs)
                .build();
    }

ruby-on-rails – 如何在YAML文件中包含YAML文件?

ruby-on-rails – 如何在YAML文件中包含YAML文件?

在YAML中有一个自定义标签用于 ruby,以在YAML文件中包含一个YAML文件?
#E.g.:  
--- !include
filename: another.yml

前一段时间曾经提出一个similar的问题,没有相关的答案.

我想知道是否有一些Ruby的自定义标签类似于this一个Python.

解决方法

如果您在Rails中,YAML可以包括ERB.

结合在一起,这里是如何使用<%=%>包括一个来自另一个的文件:

database.yml的

<% if File.exists?('/tmp/MysqL.sock') %>
<%= IO.read('config/database.MysqL.yml') %>
<% else %>
<%= IO.read('config/database.sqlite.yml') %>
<% end %>

database.sqlite.yml

sqlite: &defaults
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *defaults
  database: db/development.sqlite3

test:
  <<: *defaults
  database: db/test.sqlite3

production:
  <<: *defaults
  database: db/production.sqlite3

database.MysqL.yml

development:
  adapter: MysqL2
  # ... the rest of your MysqL configuration ...

我们今天的关于如何在JUnit 5下进行Spring Boot Test来解决在YAML文件上外部化的缩进/分层属性的分享已经告一段落,感谢您的关注,如果您想了解更多关于1.springBoot_配置_yaml文件值获取、Junit 在 spring 中报错 No tests foundmatching 是 Junit 和 spring 版本冲突导致的、org.springframework.test.context.junit4.SpringJUnit4ClassRunner的实例源码、ruby-on-rails – 如何在YAML文件中包含YAML文件?的相关信息,请在本站查询。

本文标签: