title: What is the best way to test Controllers and Services with JUnit?
body: <p>I have this Spring MVC controller:</p>
  
  <pre><code>@Controller
  @RequestMapping(value = "/foo")
  public class FooController {
  
      @Inject
      private FooService fooService;
  
      @RequestMapping(value = "foo/new")
      public final String add(final ModelMap model) {
          model.addAttribute(fooService.createFoo());
          return "foo/detail";
      }
  
      @RequestMapping(value = "foo/{id}")
      public final String detail(final ModelMap model, @PathVariable long id) {
          model.addAttribute(fooService.findById(id));
          return "foo/detail";
      }
  
      @RequestMapping(value="foo/update", method=RequestMethod.POST)
      public final String save(@Valid @ModelAttribute final Foo foo, final BindingResult result, final SessionStatus status,
              final RedirectAttributes ra, final HttpServletRequest request) {
  
          if (result.hasErrors()) {
              return "foo/detail";
          }
  
          fooService.save(foo);
          status.setComplete();
          Message.success(ra, "message.ok");
  
          return "redirect:foo/list";
      }
  
  
      @RequestMapping( value= "/foo/delete/{id}", method=RequestMethod.POST)
      public String delete(@PathVariable final Long id, final SessionStatus status, final RedirectAttributes ra, final HttpServletRequest request){
  
          if (fooService.findByIdWithOtherFoos(id).getOtherFoos().isEmpty()) {
              fooService.delete(id);
              status.setComplete();
              MessageHelper.success(ra, "message.sucess");
          } else {
              Message.error(ra, "message.error");
          }
  
          return "redirect:foo/list";
      }
  }
  </code></pre>
  
  <p>And this Service:</p>
  
  <pre><code>@Service
  @Transactional(readOnly = true)
  public class FooServiceImpl implements FooService {
  
      @Inject
      private fooRepository fooRepo;
  
      @Override
      public final Foo createFoo() {
          return new Foo();
      }
  
      @Override
      @Transactional(readOnly = false)
      public final void save(final Foo foo) {
  
          if (foo.getId() == null) {
              foo.setDate(new Date());
          }
  
          fooRepo.save(foo);
      }
  
      @Override
      @Transactional(readOnly = false)
      public final void delete(final Long id) {
          fooRepo.delete(id);
      }
  
      @Override
      public final Foo findById(final Long id) {
          return fooRepo.findOne(id);
      }
  
      @Override
      public Foo findByIdWithOtherFoos(Long id) {
          Foo foo = fooRepo.findOne(id);
          Hibernate.initialize(foo.getOtherFoos());
          return foo;
      }
  
      @Override
      public final Page&lt;Foo&gt; findAll(final Pageable pageable) {
          return fooRepo.findAll(pageable);
      }
  
      @Override
      public final Page&lt;Foo&gt; find(final String filter, final Pageable pageable) {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public final List&lt;Foo&gt; findAll(final Sort sort) {
          return fooRepo.findAll(sort);
      }
  
  }
  </code></pre>
  
  <p>What is the best way of testing with JUnit drivers and services to cover all logical conditions?
  I always end up with a bunch of test lines to cover all logical conditions.</p>
  
  <p>We recommend using MockitoJUnitRunner? Or create classes which create configuration beans. And charge them with ContextConfiguration <code>'ContextConfiguration (FooServiceImplTestConfiguration.class classes = {})'</code></p>
  
  <p>How to implement the Given-When-Then pattern?</p>