Is this Spring training useful? [closed]
开发者_开发知识库
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this questionWe have a Spring + Ibatis based J2EE app. I planned to wrap around our DAO's (which call iBatis templates ...by getting spring beans) with test cases. I don't have much experience with JUnit so I thought that simply making an object of my DAO and then calling one of the methods will work. But wrong I was, turns out that the whole J2EE app runs on appserver (container) but obviously the JUnit test cases are outside the container. So, in my test case, when I make object of the dao and call a method...it fails on a line like this which is in my DAO method
ApplicationInitializer.getApplicationContext().getBean("myMapclientBean");
So I went on a Google hunt...came across some posts and following the tubes I ended up on Spring's 4 day training course.
Wanted to get your guys opinion on what do you think about this course? Is it valuable for the price? And can a person learn this stuff on their own too? By following couple of books? (Maybe not in 4 days but, say, over a month).
oh, and I'm still not able to Unit tests these DAO's...>_<
I took the Core Spring course about a year and half ago. The syllabus apparently changed a little bit since then, though it's still very similar. The instructor was very competent. I was working with Spring prior to taking this course, but in the class I feel like I learned how to do things even better.
I think you can probably get all of the raw information out of books, Spring's online documentation, and the source code itself, but what the class does is connect everything together and teach best practices. That is, not just "how" but also "why" and "when" you should use such-and-such feature. This is why I felt like my skills improved.
The instructor was happy to answer questions about specific, real-world issues beyond the classwork. So it would be a good idea to come armed with questions.
The course isn't cheap. Whether it's worth it depends on whether you do well with classrooms versus self-teaching and utilizing online communities and peers to get that feel of best practices that books just don't offer. Also, keep in mind that Spring is constantly evolving. Although you can get a good foundation from the class, you'll still have to adapt to new features down the road (or take another class).
In answer to the second part of the question (I have not had experience of the spring course so can't comment).
You can make use of a defined application context from within Junit tests and then instantiate the DAO's as you would from within the server container either through having your test case extend AbstractTransactionalDataSourceSpringContextTests
if you are using Junit 3.8 or older.
If you are using later Junit versions (4+) you can use Spring TestContext framework and annotations.
Both explained far better than i can here from Spring and on this annotations RefCard
To solve the problem at hand:
When using Spring, your beans shouldn't really go out to the ApplicationContext
and ask for the beans they need - they should provide setters (or constructors) so that the dependencies can be injected.
It sounds like whomever designed these classes did the opposite of dependency injection.
I've used the AbstractTransactionalDataSourceSpringContextTests route. works for me. I've instantiated junits for the remoting project we have at work and this forms the basis of our junit tests. via
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
public class TestBase extends AbstractDependencyInjectionSpringContextTests
{
protected String[] getConfigLocations()
{
return new String[] { "classpath:conf/dataAccessContext.xml", "classpath:conf/applicationContext-domain.xml",
"classpath:conf/applicationContext-service.xml", "classpath:conf/applicationContext-dao.xml" };
}
protected Object getBeanToTest(String beanName)
{
return applicationContext.getBean(beanName);
}
protected Object getBeanToTest(Class clazz)
{
return applicationContext.getBean(clazz.getName());
}
}
精彩评论