My question is very similar to the question raised in Injecting Mockito mocking Spring bean . In fact, I believe that the accepted answer can really work for me. However, I have one problem with the answer, and then some further explanation in case the answer is not really my answer.
So, I followed the link in the above post on the Springockito website. I modified my test-config.xml
to include something similar to the following:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mockito="http://www.mockito.org/spring/mockito" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.mockito.org/spring/mockito http://www.mockito.org/spring/mockito.xsd"> ... <mockito:mock id="accountService" class="org.kubek2k.account.DefaultAccountService" /> ... </beans>
There seems to be something wrong with the redirect to www.mockito.org
currently, so I found the XSD code at https://bitbucket.org/kubek2k/springockito/raw/16143b32095b/src/main/resources/spring/mockito. xsd and changed the final entry in xsi: schemaLocation to point to this bitbucket link.
Running mvn test
then produced the following error (new lines added for reading):
Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 43 in XML document from class path resource [spring/test-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 43; columnNumber: 91; The prefix "mockito" for element "mockito:mock" is not bound.
So the question about Springockito: is it possible to enable this? What am I missing?
Now, for a further explanation ...
I have an interface whose implementation I'm trying to check:
public interface MobileService { public Login login(Login login); public User getUser(String accessCode, Date birthDate); }
The implementation contains a DAO for Spring @Autowire
for me:
@Service public class MobileServiceImpl implements MobileService { private MobileDao mobileDao; @Autowired public void setMobileDao(MobileDao mobileDao) { this.mobileDao = mobileDao; } }
I do not want to change my interface to include the setMobileDao
method, because it will be adding code only to support my unit testing. I am trying to mock the DAO, since the actual SUT here is ServiceImpl. How can I achieve this?