user3073074
user3073074

Reputation: 1

Assitance regarding JUnit Testing for Spring Controller Dao

I am new to Junit.Please help me to test Spring hibernate Controller with ContentType is application/json

Below is my Controller

@Controller
@RequestMapping(value="/users")
public class UserServiceImpl implements UserService{

private static Logger logger = Logger.getLogger(UserService.class);

private UserDao userDao;

@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

@RequestMapping(method = RequestMethod.POST,headers = "content-type=application/json")
@ResponseBody
public long addUser(@RequestBody UserForm user) {
logger.info("Creating new user {}"+ user);

return userDao.create(user);
}

@RequestMapping(value = "/{userId}", method = RequestMethod.GET)
@ResponseBody 
public User findUser(@PathVariable(value = "userId") String userId) {
logger.info("Reading user with id {}"+ userId);
User user = userDao.find(userId);
Validate.isTrue(user != null, "Unable to find user with id: " + userId);
return user;
}

@RequestMapping(value = "/{userId}", method = RequestMethod.PUT,headers = "content-type=application/json")
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void updateUser(@PathVariable(value = "userId") String userId, @RequestBody UserForm user) {
logger.info("Updating user with id {} with {}"+ userId +"->"+ user);
Validate.isTrue(userId.equals(user.getUserId()), "userId doesn't match URL userId: " + user.getUserId());
userDao.update(user);
}

@RequestMapping(value = "/{userId}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void deleteUser(@PathVariable(value = "userId") String userId) {
logger.info("Deleting user with id {}"+ userId);
userDao.delete(userId);
}

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public List<User> list() {
logger.info("Listing users");
return new ArrayList<User>(userDao.getUsers());
}
}

Can any one Send me the Junit Test case for Any one of the CRUD operations.

Thanks in Advance

Srikanth

Upvotes: 0

Views: 201

Answers (1)

OllesEtta
OllesEtta

Reputation: 131

If you just want to test your controller, then I would say that mock the DAO. You don't have to care about content types and such because Spring takes care of them. You are interested what the controller method is returning. If you want to test your DAO that User actually is saved to database, that's another story.

But just for testing that controller does what it is supposed to, something like this for example. Example uses EasyMock. I haven't compiled this example so it might have typos.

import static org.easymock.EasyMock.createNiceMock;

public class ControllerTest {

    private UserServiceImpl userService;
    private UserDao userDaoMock;

    @Before
    public void setup() {
        userDaoMock = createNiceMock(UserDao.class);
        userService = new UserServiceImpl();
        userSerivce.setUserDao(userDaoMock);
    }

    @Test
    public void testAddUser() {
        UserForm userForm = new UserForm();
        long expectedResult = 5L;
        expect(userDaoMock.create(userForm)).andReturn(expectedResult);
        replay(userDaoMock);

        long actualResult = userService.addUser(userForm);

        verify(userDaoMock);

        assertEquals(expectedResult, actualResult);
    }

}

Upvotes: 1

Related Questions