[英]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.