Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
While registering a user, I want to check that if the userId is unique, then only it registers, otherwise it should give an error message, I have written the following code, to check if user exists, but it is still registering a new user with duplicate userId, what am I missing? Thanks.
If the code can save a duplicate userId, it seems your database table is missing a primary key constraint or unique constraint on the userId column.
And did you already debug your code to see what happens when a duplicate user is registered? When an exception occurs (e.g. a constraint violation exception) in the register() method, you would expect false as return value but you are currently returning true.
Roel De Nijs wrote:If the code can save a duplicate userId, it seems your database table is missing a primary key constraint or unique constraint on the userId column.
And did you already debug your code to see what happens when a duplicate user is registered? When an exception occurs (e.g. a constraint violation exception) in the register() method, you would expect false as return value but you are currently returning true.
arushi tomar wrote:Yeah, even I am, that shouldn't happen. :-/
That brings us back to my original question: did you already debug your code to see what happens when a duplicate user is registered? Does the isUserExists() method returns true (as you would expect)? Or maybe it returns false for some reason?
arushi tomar wrote:Yeah, even I am, that shouldn't happen. :-/
That brings us back to my original question: did you already debug your code to see what happens when a duplicate user is registered? Does the isUserExists() method returns true (as you would expect)? Or maybe it returns false for some reason?
It returned true but still entered the part of saving user, I resolved it before I could check the problem in deep though. I will check it once again after I am done with the other work, and update the solution here.
Thanks :-)
Since you have to handle the situation where a userId exists during the insert operation anyway, there's little point in the isUserExist method.
I'd just get rid of it and let the db tell you of the ConstraintViolation (which you're handling) when you try the insert.
straws are for suckers. tiny ads are for attractive people.
Free, earth friendly heat - from the CodeRanch trailboss