首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单元测试中的两个弹簧横幅

单元测试中的两个弹簧横幅
EN

Stack Overflow用户
提问于 2022-10-12 13:25:50
回答 1查看 66关注 0票数 0

我的弹簧靴测试有奇怪的问题。当我运行它时,控制台上有两个弹簧横幅:

代码语言:javascript
复制
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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“选项卡,我看到两个元素:

代码语言:javascript
复制
MyApplication (autotedected)
MyApplication MVC  (autotedected)

我的测试课程如下所示

代码语言:javascript
复制
@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看起来就像

代码语言:javascript
复制
@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;
    }
    ...

这怎么可能呢?有问题吗?

EN

回答 1

Stack Overflow用户

发布于 2022-10-12 13:35:52

尝试删除@AutoConfigureMockMvc@ContextConfiguration(classes = {MockMvcTestConfig.class})并尝试运行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74042642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档