Announcement Announcement Module
Collapse
No announcement yet.
[Junit4] : How to handle class level ref value for CRUD operation in junit4 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Junit4] : How to handle class level ref value for CRUD operation in junit4

    Hi,

    I am using Junit 4 with spring 2.5 and hibernate3 for unit test case. I want to test the createXXX, getXXX, updateXXX and delete operation consecutively. First of all I want to test createXXX operation it returns a Id of created object, further more I want to test get/update/delete the same object by returning id. I could not maintain the that value in test case, means after executing one test method it get fresh. Please guide me how to keep value of id which return at createXXX, code is as follows.

    Code:
    public class UserServiceTest extends AbstractDependencyInjectionSpringContextTests  { 
    
    	static Integer USER_ID = null;
    		
    	@Autowired
    	protected UserService userService;
    
       // specifies the Spring configuration to load for this test fixture
        protected String[] getConfigLocations() {
            return new String[] { "/applicationTestContext.xml","/applicationTestContext-additional.xml" };
        }
    
    	@Test
    	public final void testAddUser() {
    		User user = prepareUser(); // populate the info in user object.
    				
    		USER_ID = this.userService.addUser(user);
    		assertNotNull(USER_ID);
    		
    	}
    
    	@Test
    	public final void testGetUserById() {
    		User user = this.userService.getUserById(USER_ID);
    		assertNotNull(user);
    	}
    
    	@Test
    	private void testUpdateUser() {
    		User user = this.userService.getUserById(USER_ID);
    		
    		user.setUpdatedBy("updated by name");
    		this.userService.updateUser(user);
    		
    		user = this.userService.getUserById(USER_ID);
    		assertNotNull(user.getUpdatedBy());
    	}
    
    	@Test
    	@ExpectedException(value = ObjectNotFoundException)
    	private void testDeleteUser() {
    		User user = this.userService.getUserById(USER_ID);
    		this.userService.deleteUser(user);
    		try {		
    			user = this.userService.getUserById(USER_ID);
    			assertNull(user);
    		} catch(UsernameNotFoundException unfe) {
    			assertTrue(true);
    		} catch (ObjectNotFoundException objNFEx) {
    			assertTrue(true);
    		} catch (Exception ex ) {
    			assertTrue(true);
    		}
    		
    	}
    }

    Thanks..

    Ranjeet
Working...
X