java - Unit testing controllers with annotations -
i'm trying write unit tests controllers in spring mvc web app. have got comprehensive set of unit tests domain model, completeness want test controllers too.
the issue i'm facing trying test them without loading spring context. thought around mocking, method in controller has @transactional annotation attempts open transaction (and fails nullpointerexception because no spring context loaded).
code example:
public class userscontroller { @autowired private usermanager usermanager; @transactional @requestmapping(method = requestmethod.post) public modelandview create(user user) { usermanager.save(user); modalandview mav = new modelandview(); mav.addobject("user", user); mav.setviewname("users/view"); return mav; } }
so want test behaviour without loading context , persisting user.
does have ideas on how can achieve this?
cheers,
caps
i'd mocking way go here. @transactional
annotation have no effect unless there spring context loaded , instructed configure annotation-based transactions.
make sure aren't instructing junit run test within spring context specifying like:
@contextconfiguration(locations = "classpath:spring/itestassembly.xml") @runwith(springjunit4classrunner.class)
to prevent confusion, keep unit tests (not running in spring context) in separate files integration tests. typically mocking occurs in unit tests , not in integration tests.
Comments
Post a Comment