Announcement Announcement Module
Collapse
No announcement yet.
Spring 3 Unit Testing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3 Unit Testing

    Hi,

    I'm presently updating some Spring 2.5/JUnit 3.8 unit tests to Spring 3/JUnit 4.9.

    Perhaps my configuration is incorrect, but I find Spring 3's unit testing framework a little more verbose than expected. See my 2.5 and 3 configurations below.
    For Spring 3, have I added any unnecessary configuration? Any feedback is appreciated

    Thanks

    Spring 2.5

    Code:
    public class UserDaoImplTest extends AbstractDependencyInjectionSpringContextTests
    {
    
    	private UserDao userDao;
    	private UserDao userDaoInvalidDataSource;
    	private UserDao userDaoWithMockProcessor;
    	private Cache cache;
    	private ReentrantReadWriteLock readWriteLock;
    	private UserProfileCacheProcessor userProfileCacheProcessorMock;
    
    	public UserDaoImplTest()
    	{
    		setAutowireMode(AUTOWIRE_BY_NAME);
    	}
    
    
    	@Override
    	protected String[] getConfigLocations()
    	{
    		return new String[] { "classpath:spring/spring-test-ibatis.xml" };
        }
    
    	public void onSetUp()
    	{
    		userDao.refreshCache();
    	}
    
    	public void OnTearDown()
    	{
    		cache.flush();
    	}
    
    	public void testRefreshCache()
    	{
    		assertEquals("...", 3, cache.getSize());
    	}
    Spring 3.0

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("/spring/spring-test-ibatis.xml")
    public class UserDaoImplTest extends Assert
    {
    	@Resource
    	private UserDao userDao;
    	@Resource
    	private UserDao userDaoInvalidDataSource;
    	@Resource
    	private UserDao userDaoWithMockProcessor;
    	@Resource
    	private Cache cache;
    	@Resource
    	private ReentrantReadWriteLock readWriteLock;
    	@Resource
    	private UserProfileCacheProcessor userProfileCacheProcessorMock;
    
    
    	@Before
    	public void onSetUp()
    	{
    		userDao.refreshCache();
    	}
    
    	@After
    	public void OnTearDown()
    	{
    		cache.flush();
    	}
    
    	@Test
    	public void testRefreshCache()
    	{
    		assertEquals("...", 3, cache.getSize());
    	}

  • #2
    I'd say you'd better not extend Assert but use static imports instead. And I prefer @Inject to @Resource or @Autowired. But that's it, more or less.

    You may have to type a few more characters than with JUnit 3 + Spring 2.5, but I wouldn't call that verbose. The new annotations make much clearer what's going on. You can see which members get injected, and you don't have to rely on naming conventions for test methods and fixtures.

    And you're no longer forced to extend any base class.

    Regards,
    Harald

    Comment

    Working...
    X