我的弹簧靴测试有奇怪的问题。当我运行它时,控制台上有两个弹簧横幅:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.6)
Skipping auto-registration
Default master password will be used for encryption
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.6)
Skipping auto-registration看起来有两个上下文被加载了。然后打开"Spring“选项卡,我看到两个元素:
MyApplication (autotedected)
MyApplication MVC (autotedected)我的测试课程如下所示
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {"spring.main.allow-bean-definition-overriding=true"})
@ContextConfiguration(classes = {MockMvcTestConfig.class})
@AutoConfigureMockMvc
@ActiveProfiles("mvcTest")
class AuthControllerTest {
@Autowired
private MockMvc mockMvc;
. . .
}MockMvcTestConfig看起来就像
@TestConfiguration
@EnableTransactionManagement
@MapperScan
@Profile("mvcTest")
public class MockMvcTestConfig {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder();
return embeddedDatabaseBuilder
.setType(EmbeddedDatabaseType.H2)
.build();
}
@Bean
public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public SpringLiquibase springLiquibase() {
SpringLiquibase springLiquibase = new SpringLiquibase();
springLiquibase.setDataSource(dataSource());
springLiquibase.setChangeLog("classpath:/my.json");
return springLiquibase;
}
...这怎么可能呢?有问题吗?
发布于 2022-10-12 13:35:52
尝试删除@AutoConfigureMockMvc和@ContextConfiguration(classes = {MockMvcTestConfig.class})并尝试运行。
https://stackoverflow.com/questions/74042642
复制相似问题