Done: http://jira.springframework.org/browse/SPR-4699
Printable View
One possible solution for using mocks with autowiring is to use the FactoryBean interface that has a getObjectType method (on which spring operates), iv written a post on the subject that can be found on my blog (google javadevelopmentforthemasses, i cant add links yet).
Thanks a lot for the post here and your blog. I immediately changed our mocks to your factory and even enhanced our generic DAO factory with our solution and now autowiring works like a charm...
I also like your approach with the AutoBeanDeclarer - however it does not seem to be suitable for us: we have several places, where we need real implementations (e. g. for the login, masterdata, etc.) and mocks (e. g. within the functionality to test) together. But as your implementation takes care of each and every autowired field, your cannot mix things here... (?)
We thought about an easy way to overwrite/replace specific beans in the application context with mocks. Our current solution is having a single xml-file for each interface to mock only defining this single mock. The test then has a @ContextConfiguration specifiying the global application context xml file and overwriting/replacing specific beans with mocks using the above simple xml files. The test configuration to replace the systemDAO and the userDAO with mocks would look like this:
But with your idea I've started thinking about a annotation based variation:Code:@ContextConfiguration(locations = { "classpath:config/spring-context.xml",
"classpath:mock/systemDAO.xml", "classpath:mock/userDAO.xml" })
Create a post processor scanning the test classes for a, say "@Mock" annotation specifiying all the interfaces to be mocked. Something like this:
Any thoughts on this?Code:@ContextConfiguration(locations = { "classpath:config/spring-context.xml" })
@Mock(interfaces = { SystemDAO.class, UserDAO.class })
Sorry for not replying sooner, as for more idea you can make the AutoBeanDeclarer SpringContextAware and make sure that it wont override existing bean definitions that were already defined in the xml context, not only that but iv created an XML free testing framework that hopefully ill be able to post about in the near future (by using a custom Loader).
Sorry for not replying sooner
As for more ideas, you can make AutoBeanDeclarer to be Spring context aware and check that for each mock added there isn't already an existing bean in the context, iv taken it further by using a custom Loader i was able to create an XML free framework that registers mocks for all Spring based tests (i should write an entry about it).
I am new to JAVA programming and I am trying to create a portlet in Eclipse using Maven, Hibernate and Springframe. I get the same type of error message (see below) when I try to deploy my portlet to LIFERAY. I am confused by what I have done wrong. Can anyone PLEASE help me? Here is the error message and some of my code/situation:
13:13:23,543 ERROR [ContextLoader:215] Context initialization failed
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'projectTasksPortlet': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private edu.fccc.psf.projecttasks.services.TimeAcctService
edu.fccc.psf.projecttasks.portlet.ProjectTasksPort let.timeAcctService; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'timeAcctService': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private edu.fccc.psf.projecttasks.dao.ProjectsUsersDao edu.fccc.psf.projecttasks.services.TimeAcctService .projectsUsersDao; nested exception is org.springframework.beans. factory.NoSuchBeanDefinitionException: No unique bean of type [edu.fccc.psf.projecttasks.dao.ProjectsUsersDao] is defined: Unsatisfied dependency of type [interface edu.fccc.psf.projecttasks.dao.ProjectsUsersDao]: expected at least 1 matching bean
I created my data access objects (I believe the term I was told is DAO/TO). I then created my services "TimeAcctService" which accesses one of my DAOs using
@Service
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class TimeAcctService {
@Autowired
private ProjectsUsersDao projectsUsersDao;
@Transactional(propagation = Propagation.REQUIRED)
public void saveProjectsUsers(ProjectsUsers projectsUsers) {
projectsUsersDao.saveProjectsUsers(projectsUsers);
}
@Transactional(propagation = Propagation.REQUIRED, readOnly=false)
public void deleteProjectsUsers(ProjectsUsers projectsUsers) {
projectsUsersDao.deleteProjectsUsers(projectsUsers );
}
@Transactional(propagation = Propagation.REQUIRED)
public List getProjectsUsers (String username) {
return projectsUsersDao.getProjectsUsers(username);
}
public ProjectsUsers getProjectsUsers (Integer record) {
return projectsUsersDao.getProjectsUsers(record);
}
}
Why does the @Autowired fail. Is the problem in one of my configuartion files or have I done something else wrong? PLEASE help me.
Thanks again for clarifying.
Is there a way to tell spring which class/interfaces objects returned by factory-method implement? This means overwriting the default reflection mechanism? Something like
____________________
watch movies online
Sorry I don't understand your reply. I have my getters and setters for my DAO. Why do I keep getting the following error message:
18:19:02,744 ERROR [ContextLoader:215] Context initialization failed
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'timeAcctService': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private edu.fccc.psf.projecttasks.dao.ProjectsUsersDao edu.fccc.psf.projecttasks.services.TimeAcctService .projectsUsersDao; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'projectsUsersDao' defined in ServletContext resource [/WEB-INF/portlet-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'dataSource' of bean class [edu.fccc.psf.projecttasks.dao.ProjectsUsersHiberna teDao]: Bean property 'dataSource' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Basically use ReflectionTestUtils
http://hillert.blogspot.com/2008/04/...ing-25-to.html