java启动集成测试时提示 ERROR o.s.b.SpringApplication - [reportFailure,870] - Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dlabLogController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'operatorLogServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'activitiProcessServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'converUtil': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sysUserServiceImpl': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sysUserMapper' defined in file [D:\SERES_workSpace\dlab-backend\dlab-system\dlab-system-impl\target\classes\cn\seres\dlab\system\mapper\SysUserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [cn/seres/dlab/framework/config/MyBatisConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.lang.NullPointerException
使用 @ActiveProfiles
如果你想指定一个特定的 profile,例如 dev
,并且在 application-dev.yml
中定义了这些 profile 的配置,你可以使用 @ActiveProfiles
注解。
示例
假设你有一个 application-dev.yml
文件,并且你想激活 dev
profile,你可以这样做:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import com.example.Application; // 你的主应用类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DLABApplication.class)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("dev")
@Rollback(false)
class TrialEntrustServiceIntegrationTest {
@Autowired
private ITrialEntrustService trialEntrustService;
// 测试方法
@Test
void shouldProcessTrialEntrust() {
// 测试逻辑
}
}
注意事项
@SpringBootTest
: 这个注解用于启动一个完整的 Spring Boot 应用环境,它会自动配置并启动所有 Spring 组件。properties
属性: 你可以使用properties
属性来指定配置文件的位置。@ActiveProfiles
: 这个注解用于激活特定的 profiles,这使得 Spring Boot 能够加载相应的配置文件。@AutoConfigureTestDatabase
: 这个注解用于配置测试时使用的数据库。在这里,我们设置了replace = AutoConfigureTestDatabase.Replace.NONE
,这意味着使用你在application-dev.yml
中配置的数据库连接。@Rollback(false)
: 这个注解用于禁用事务回滚,这样可以在测试之间保留数据。但是在实际单元测试中,通常使用@Rollback(true)
来确保每个测试方法都在一个干净的状态下运行。
示例配置文件
假设你的 application-dev.yml
文件如下所示:
# application-dev.yml
spring:
datasource:
url: jdbc:h2:mem:testdb_dev;DB_CLOSE_DELAY=-1
driver-class-name: org.h2.Driver
username: sa
password:
jpa:
show-sql: true
hibernate:
ddl-auto: create
properties:
hibernate:
dialect: org.hibernate.dialect.H2Dialect
flyway:
locations: classpath:db/migration_dev
logging:
level:
root: INFO
cn.seres.dlab: DEBUG
总结
通过上述方法之一,你应该能够指定有效的配置文件并在单元测试中使用它。这可以帮助你在测试期间使用不同的数据库连接或其他配置设置,从而确保测试的隔离性和准确性。如果你需要进一步的帮助或有其他具体需求,请随时提问。