Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (120 points)

I am mocking a service layer and stubbing one of the method but doReturn doesnt return the mocked value. Bellow is my code, what i am doing wrong

public class Service
 {
   @Autowired
    Dao dao;
   ....
   public int doSomething(){
      return 2;
   }
 }

@Category(IntegrationTest.class)
@RunWith(SpringRunner.class)
public class Test{

   @Autowired
   Service instanceOfService;

  @Test
  public void testMethod(){

   Service mockedService = Mockito.spy(instanceOfService)
   Mockito.doReturn(4).when(mockedService).doSomething();
   Assert.assertEquals(4, mockedService.doSomething());

    }
}

Please log in to answer this question.

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...