Java @使用测试属性(测试数据库)文件运行SpringBootTest

Java @使用测试属性(测试数据库)文件运行SpringBootTest,java,spring-boot,testing,Java,Spring Boot,Testing,我有一个关于SpringBoot2的项目,我想测试它。 表: 请告诉我,我如何切换到测试基地?我正在尝试@PropertySourceclasspath:application-test.properties和@TestPropertySourceclasspath:application-test.properties,但无法使用Spring概要文件测试运行 -Dspring.profiles.active=测试 您可以将默认配置文件作为测试添加到application.yml中,以自动拾取它

我有一个关于SpringBoot2的项目,我想测试它。 表:


请告诉我,我如何切换到测试基地?我正在尝试@PropertySourceclasspath:application-test.properties和@TestPropertySourceclasspath:application-test.properties,但无法使用Spring概要文件测试运行

-Dspring.profiles.active=测试

您可以将默认配置文件作为测试添加到application.yml中,以自动拾取它

春天:
profiles.active:test

U意思是,用@Profile创建@Configuration,在这个配置中,@PropertySource可以工作吗?你能给我举个例子吗?我明白了,我做到了。谢谢
@Entity
@Table(name = "Contract")
public class Contract extends ADBObjectWithID<ContractBean>
{
    @NotBlank
    @Size(max = 512)
    private String name;

    @Size(max = 2056)
    private String comment;

    @Override
    public ContractBean toBean()
    {
        return new ContractBean(getId(), getName(), getComment());
    }
}
@Service
public class ContractServiceImpl implements ContractService
{
    private ContractRepository contractRepository;

    public ContractServiceImpl(ContractRepository contractRepository)
    {
        this.contractRepository = contractRepository;
    }

    @Override
    @Transactional
    public Contract saveObject(ContractBean contractBean)
    {
        Contract contract;
        if (contractBean.getId() == null)
        {
            contract = new Contract();
        }
        else
        {
            contract = findById(contractBean.getId()).orElseThrow(() -> new NullPointerException("Contract not found"));
        }
        contract.setName(contractBean.getName());
        contract.setComment(contractBean.getComment());
        return contractRepository.save(contract);
    }

    @Override
    @Transactional
    public void deleteObject(ContractBean contractBean)
    {

    }

    @Override
    public Optional<Contract> findById(Long id)
    {
        return contractRepository.findById(id);
    }
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class ContractTest
{
    @Autowired
    private ContractService contractService;

    @Test
    public void createContract()
    {
        String name = "Contract name";
        String comment = "Contract comment";

        ContractBean contractBean = new ContractBean();
        contractBean.setName(name);
        contractBean.setComment(comment);

        Contract contract = contractService.saveObject(contractBean);
        Assert.assertEquals(name, contract.getName());
        Assert.assertEquals(comment, contract.getComment());

        contractBean = contract.toBean();
        Assert.assertEquals(name, contractBean.getName());
        Assert.assertEquals(comment, contractBean.getComment());
    }
}