简体   繁体   English

Spring Boot-测试用例-不加载所有组件

[英]Spring Boot - Test Cases - Dont Load All Components

I am trying to to rest my rest classes in Spring MVC 我试图在Spring MVC中休息我的休息课

If I run the following code (ran fine when the project was small but now fails) it tries to load all the different components in my application. 如果我运行以下代码(当项目很小但现在失败时可以运行),它将尝试加载应用程序中的所有不同组件。 This includes beans which interact with external systems and need credentials in order to connect 这包括与外部系统交互并需要凭据才能连接的bean

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class TestDummyRest extends BaseRestTestCase{

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private IDummyServices mockDummyServices;


    @Test
    public void getSendGoodMessage() throws Exception {

        given(mockDummyServices.sendGoodMessage(Mockito.anyString())).willReturn(true);

        mockMvc.perform(get("/dummy"))
                    .andExpect(status().isOk())
                    .andExpect(content().contentType(TEXT_PLAIN_CONTENT_TYPE));

        verify(mockDummyServices, times(1)).sendGoodMessage(Mockito.anyString());
    }
}

How do I tell my test classes not to load the @Configuration or @Component classes of my application? 如何告诉测试类不要加载应用程序的@Configuration或@Component类?

Instead of not creating other classes in your application, you could only create the classes you are interested in, see 15.6.1 Server-Side Tests - Setup Options 除了只能在您的应用程序中不创建其他类之外,您还可以创建自己感兴趣的类,请参见15.6.1服务器端测试-设置选项

The second is to simply create a controller instance manually without loading Spring configuration. 第二个方法是简单地手动创建控制器实例,而无需加载Spring配置。 Instead basic default configuration, roughly comparable to that of the MVC JavaConfig or the MVC namespace, is automatically created and can be customized to a degree: 相反,会自动创建基本的默认配置(可以大致与MVC JavaConfig或MVC命名空间的配置相媲美),并可在一定程度上进行自定义:

public class MyWebTests {

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.standaloneSetup(new AccountController()).build();
    }

    // ...

}

您需要为此使用@TestComponent@TestConfiguration ,如Spring doc 此处所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为具有自动装配组件的Spring Boot Service应用程序编写JUnit测试用例 - Writing JUnit test cases for a Spring Boot Service Application with autowired components Spring boot @Value 属性在测试用例中为空 - Spring boot @Value property is null in test cases Spring Boot 应用程序的单元测试用例 - unit Test cases for spring boot application Spring 引导:在测试中加载实现接口的所有 beans? - Spring Boot: load all beans implementing an interface in test? 如何加载,在 Spring 引导测试中,基本 Spring 应用程序上下文仅添加命名组件? - How to Load, in Spring Boot Test, Basic Spring Application-Context Adding Only Named Components? 如何在Spring Boot中的所有测试案例之前仅设置一次独立Controller? - How to setup standalone Controller only once before all test cases in Spring Boot? Spring Boot Test - 我不想为特定测试加载所有@Configuration类 - Spring Boot Test - I do not want to load all @Configuration classes for a particular test 如何为 spring 启动应用程序编写 Junit 测试用例? - How to write Junit test cases for spring boot application? 在单元测试用例 Spring 引导期间模拟 API 调用 - Mock API calls During Unit Test cases Spring boot 如何使用spring-boot框架运行测试用例 - How to run test cases with the spring-boot framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM