我有两个类:
public MyService {
@Autowired
private MyDao myDao;
private List<Items> list;
@PostConstruct
private void init(){
list = myDao.getItems();
}
}现在,我想在单元测试中引入MyService,因此我将模拟行为MyDao。
XML:
<bean class = "com.package.MyService">
<bean class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.package.MyDao"/>
</bean>
<util:list id="responseItems" value-type="com.package.Item">
<ref bean="item1"/>
<ref bean="item2"/>
</util:list>单元测试:
@ContextConfiguration("/test-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class MyServiceTest {
@Autowired
MyService myService
@Autowired
MyDao myDao;
@Resource
@Qualifier("responseItems")
private List<Item> responseItems;
@Before
public void setupTests() {
reset(myDao);
when(myDao.getItems()).thenReturn(responseItems);
}
}这样做的问题是创建了MyService bean,并且在定义模拟行为之前调用了它的@PostConstruct bean。
如何在XML中定义模拟行为,或者将@PostConstruct延迟到单元测试设置之后?
发布于 2017-06-08 22:15:13
我在我的项目中也有同样的要求。我需要使用@PostConstructor设置一个字符串,而我不想运行spring上下文,换句话说,我想要简单的mock。我的要求如下:
public class MyService {
@Autowired
private SomeBean bean;
private String status;
@PostConstruct
private void init() {
status = someBean.getStatus();
} }
解决方案:
public class MyServiceTest(){
@InjectMocks
private MyService target;
@Mock
private SomeBean mockBean;
@Before
public void setUp() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
MockitoAnnotations.initMocks(this);
when(mockBean.getStatus()).thenReturn("http://test");
//call post-constructor
Method postConstruct = MyService.class.getDeclaredMethod("init",null); // methodName,parameters
postConstruct.setAccessible(true);
postConstruct.invoke(target);
}
}发布于 2016-07-05 03:07:34
MyDao听起来像是一个外部系统的抽象。通常,不应该在@PostConstruct方法中调用外部系统。而是让MyService中的另一个方法调用您的getItems()。
Mockito注入将在Spring启动之后进行,此时mock不会像您看到的那样工作。您不能延迟@PostConstruct。为了避免这种情况并让加载程序自动运行,让MyService实现SmartLifecycle并在start()中调用getItems()。
https://stackoverflow.com/questions/38175822
复制相似问题