Nep*_*daj 53 java junit spring dependency-injection
我有一个我想测试的Spring组件,这个组件有一个autowired属性,我需要更改它以进行单元测试.问题是,该类在后构造方法中使用自动装配的组件,因此在实际使用之前我无法替换它(即通过ReflectionTestUtils).
我该怎么办?
这是我要测试的类:
@Component
public final class TestedClass{
@Autowired
private Resource resource;
@PostConstruct
private void init(){
//I need this to return different result
resource.getSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试用例的基础:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= "classpath:applicationContext.xml")
public class TestedClassTest{
@Autowired
private TestedClass instance;
@Before
private void setUp(){
//this doesn't work because it's executed after the bean is instantiated
ReflectionTestUtils.setField(instance, "resource", new Resource("something"));
}
}
Run Code Online (Sandbox Code Playgroud)
在调用postconstruct方法之前,有没有办法用其他东西替换资源?想告诉Spring JUnit跑步者自动装配不同的实例?
Ann*_*nko 52
你可以使用Mockito.我不确定PostConstruct
具体,但这通常有效:
// Create a mock of Resource to change its behaviour for testing
@Mock
private Resource resource;
// Testing instance, mocked `resource` should be injected here
@InjectMocks
@Resource
private TestedClass testedClass;
@Before
public void setUp() throws Exception {
// Initialize mocks created above
MockitoAnnotations.initMocks(this);
// Change behaviour of `resource`
when(resource.getSomething()).thenReturn("Foo");
}
Run Code Online (Sandbox Code Playgroud)
我创建了关于该主题的博客文章.它还包含带有工作示例的Github存储库的链接.
诀窍是使用测试配置,你可以用假的覆盖原始的spring bean.你可以使用@Primary
和@Profile
注释这个技巧.
集成测试的另一种方法是定义一个新的 Configuration 类并将其作为您的@ContextConfiguration
. 在配置中,您将能够模拟您的 bean,并且您还必须定义在测试流程中使用的所有类型的 bean。举个例子:
@RunWith(SpringRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class MockTest{
@Configuration
static class ContextConfiguration{
// ... you beans here used in test flow
@Bean
public MockMvc mockMvc() {
return MockMvcBuilders.standaloneSetup(/*you can declare your controller beans defines on top*/)
.addFilters(/*optionally filters*/).build();
}
//Defined a mocked bean
@Bean
public MyService myMockedService() {
return Mockito.mock(MyService.class);
}
}
@Autowired
private MockMvc mockMvc;
@Autowired
MyService myMockedService;
@Before
public void setup(){
//mock your methods from MyService bean
when(myMockedService.myMethod(/*params*/)).thenReturn(/*my answer*/);
}
@Test
public void test(){
//test your controller which trigger the method from MyService
MvcResult result = mockMvc.perform(get(CONTROLLER_URL)).andReturn();
// do your asserts to verify
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
87698 次 |
最近记录: |