title: How can I test functions that have multiple returns?
body: <p>I'm currently learning java and I am making an e commerce website. I am currently in the process of writing some tests. My question is that one of my mappings in my controller returns a different page based on the length of a list called results. It's a search item function, if the item doesn't exist it takes you back to the search item page, if there is one item with that name it takes you to a different page and if there is multiple instances of this item it returns you to a third page. The mapping is below.</p>
  <pre><code>@RequestMapping(&quot;/findItemByName&quot;)
      public String processFindForm(Item item, BindingResult result, Map&lt;String, Object&gt; model) {
          // allow parameterless GET request for /products to return all records
          if (item.getItemName() == null) {
              item.setItemName(&quot;&quot;); // empty string signifies broadest possible search
          }
  
          // find products by name
          List&lt;Item&gt; results = itemRepo.findByItemName(item.getItemName());
          if (results.isEmpty()) {
              // no products found
              result.rejectValue(&quot;name&quot;, &quot;notFound&quot;, &quot;not found&quot;);
              return &quot;finditem&quot;;
          } else if (results.size() == 1) {
              // 1 product found
              item = results.iterator().next();
              model.put(&quot;item&quot;, item);
              return &quot;showitem&quot;;
          } else {
              // multiple products found
              model.put(&quot;items&quot;, results);
              return &quot;showitems&quot;;
          }
  
      }
  </code></pre>
  <p>No matter what I seem to do I can only get the function to return &quot;finditem&quot; whilst testing. I have this as my code so far in my class for testing.</p>
  <pre><code>@Mock
      Model model;
      @Mock
      Item item;
      @Mock
      Item item2;
      @Mock
      ItemRepository itemRepo;
      @Mock
      Map&lt;String,Object&gt; mockMap;
      @Mock
      BindingResult mockResult;
      @Mock
      List&lt;Item&gt; results;
      
      @InjectMocks
  
  @Test
      public void testFindByItemNameReturnsFindItemWhenListLengthOne() {
          
          results.add(item);
          String expectedValue = &quot;showitem&quot;;
          String actualValue = itemController.processFindForm(item, mockResult, mockMap);
          
          Assert.assertEquals(expectedValue, actualValue);
      }
      
      @Test
      public void testFindByItemNameReturnsFindItemWhenListLengthTwo() {
          List&lt;Item&gt; results = new ArrayList&lt;&gt;();
          
          results.add(item2);
          results.add(item2);
          System.out.print(results.size());
          String expectedValue = &quot;showitems&quot;;
          String actualValue = itemController.processFindForm(item, mockResult, mockMap);
          
          Assert.assertEquals(expectedValue, actualValue);
      }
  </code></pre>
  <p>How do I 'inject' (if that is the correct word) result into the function to change my actualValue?</p>
  <p>Thanks in advance</p>