Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用单独的application.properties创建Spring引导测试_Java_Spring_Hibernate_Spring Boot_Spring Test - Fatal编程技术网

Java 使用单独的application.properties创建Spring引导测试

Java 使用单独的application.properties创建Spring引导测试,java,spring,hibernate,spring-boot,spring-test,Java,Spring,Hibernate,Spring Boot,Spring Test,我正在用SpringBoot开发web应用程序,现在我正在尝试为DAO层创建测试,我想使用不同的配置,这些配置将读取自定义属性文件,而不是标准属性文件。但我有这个问题,它总是读取默认应用程序。和hibernate.properties 用户希望这样做,以便为测试提供不同的hibernate.ddl-auto属性。但是当我运行测试时,我看到Spring从resource文件夹中的hibernate.properties读取属性(我故意在该文件中输入了一个拼写错误,以便在Spring读取时得到异常)

我正在用SpringBoot开发web应用程序,现在我正在尝试为DAO层创建测试,我想使用不同的配置,这些配置将读取自定义属性文件,而不是标准属性文件。但我有这个问题,它总是读取默认应用程序。和hibernate.properties
用户希望这样做,以便为测试提供不同的hibernate.ddl-auto属性。但是当我运行测试时,我看到Spring从resource文件夹中的hibernate.properties读取属性(我故意在该文件中输入了一个拼写错误,以便在Spring读取时得到异常)。但为什么即使在我使用
@TestPropertySource
时,它也会读取该文件呢
我知道有些事情我不知道,但是什么呢? 包src/test/java/com.guard/dao

@RunWith(SpringRunner.class)
@DataJpaTest
@Rollback

public class LifeguardDaoTest {
    @Autowired
    private LifeguardDao lgDao;

    @Test
    public void selectTest(){
        for (Lifeguard lg :lgDao.getAll()) {
            System.out.println(lg);
        }
    }
}`
测试配置类用于设置上下文 包src/test/java/com.guard

@SpringBootApplication
@EntityScan(value = {"com.guard.dao","com.guard.model"})
@TestPropertySource(value = {"classpath:application-test.properties", "classpath:hibernate-test.properties"})
public class TestConfiguration {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(TestConfiguration.class, args);
        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        System.out.println("Spring boot test generated " + beanNames.length + " beans");

    }
}
所需的application-test.properties和hibernate-test.properties位于src/test/java路径上
这是项目结构(对不起,我不知道如何在这里设计)

我的application-test.properties `


`

在测试目录中创建新的资源目录,并将测试属性文件放在那里。同时将属性文件重命名为application.properties和hibernate.properties


Spring测试将从test/resources/目录获取属性。在这种方法中,您不需要@TestPropertySource,通常,@TestPropertySource与@ContextConfiguration一起使用

尝试使用此配置类

@Configuration
@ComponentScan
@Profile("test")
public class TestConfiguration {

}
及注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ContextConfiguration(classes = TestConfiguration.class)
@ActiveProfiles("test")
@TestPropertySource(locations="classpath:application-test.properties")
public @interface IntegrationTest { }
然后您只需像这样编写测试:

@RunWith(SpringJUnit4ClassRunner.class)
@IntegrationTest
public class SomeDaoTest {
...
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ContextConfiguration(classes = TestConfiguration.class)
@ActiveProfiles("test")
@TestPropertySource(locations="classpath:application-test.properties")
public @interface IntegrationTest { }
@RunWith(SpringJUnit4ClassRunner.class)
@IntegrationTest
public class SomeDaoTest {
...
}