title: Junit how to mock method return in test method?
body: <p>I was getting a NullPointerException with following method's test but after comments I edited my code and now getting following error:</p>
  
  <p>org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ...</p>
  
  <p>My source code is at <em>src/main/java</em> and test is at <em>src/test/java</em> but this doesnt play much of a big role, I moved the test class in main/java and didnt help.</p>
  
  <pre><code>@Component
  public class MyClass {
  
  @Autowired
  MyService myService;
  
  public void myMethod(Dog dog, Animal animal) { 
      if (myService.isAnimal(dog.getStatus()) {//NPE was on this line
        dog.setName("mike");
      } else {
        dog.setName(null);
      }
  }
  }
  </code></pre>
  
  <p>Below is test code:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = MyClass.class)
  public class MyClassTest {
  
  @Autowired
  MyClass testObjMyClass;
  
  @Test
  public void testMyMethod() {
  
  MyService myService = mock(MyService.class);
  
  Dog dog = new Dog();
  dog.setStatus("Y"); // this should give true for isAnimal()
  when(myService.isAnimal(dog.getStatus())).thenReturn(true); // I tried with ("Y") as well
  
  testObjMyClass.myMethod(dog, animal);// I defined animal in test Class variables before.
  assertEquals("mike", dog.getName());
  }
  
  }
  </code></pre>
  
  <p>My project is springboot application, myService is autowired in myMethod(). I would appreciuate your tips!</p>