0

below is my controller:

    @RestController
    @RequestMapping("/Employee")
    public class Employeecontroller {
        
        @Autowired
        Employeeservice empservice;
        
    
        @PostMapping("/addEmployee")   // adding emp details into the table
        public Employee addemployee(@RequestBody Employee emp)
        {
            empservice.saveemp(emp);
            return emp; 
        }
    }
  1. this is the empservice class:
    @Service
        public class Employeeservice {
            
            @Autowired
            EmployeeRepository emprepo;
        
            public Employee saveemp(Employee emp) {
                System.out.println("inside save emp");
                 return emprepo.save(emp); 
            }
        }

(here i dont want to call to emprepo.save(emp) method, which is a database call, so i used Mockito.when and then methods in below test class) below is the test class:

    @SpringBootTest
    @RunWith(MockitoJUnitRunner.class)
    @AutoConfigureMockMvc
    class RestServiceApplicationTests {
    
        @Autowired
        private MockMvc mvc;
    
        @Autowired
        ObjectMapper objectMapper;
    
        @MockBean
        Employeeservice empservice;
    
        Employee reqemp = new Employee("Bob", "java");
    
        @Test
        public void testaddemp() throws Exception {
    
            when(empservice.saveemp(reqemp)).thenReturn(new Employee(1, "Bob", "java"));
    
            RequestBuilder request = MockMvcRequestBuilders.post("/Employee/addEmployee")
                    .contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(reqemp));
    
            MockHttpServletResponse returnedemp = (MockHttpServletResponse) mvc.perform(request).andExpect(status().isOk())
                    .andReturn().getResponse();
    
            Employee expectedemp = new Employee(1, "Bob", "java");
    
            assertEquals(objectMapper.writeValueAsString(expectedemp), returnedemp.getContentAsString());
    
        }
    }

Testcase failed with:

    org.opentest4j.AssertionFailedError: expected: <{"id":1,"name":"Bob","tech":"java"}> but was: <{"id":0,"name":"Bob","tech":"java"}>
        at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
        at org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
        at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1124)
        at com.easytocourse.demo.RestServicewithActuatorApplicationTests.testaddemp(RestServicewithActuatorApplicationTests.java:55)

when i use @Mock or @SpyBean it is returning expected employee object

Please help me in understanding why @MockBean is not working?

Please clarify the below points

1. what is the difference between @Mock, @MockBean, @SpyBean, @InjectMock annotations, when to use these annotations?

Bharath
  • 81
  • 1
  • 11
  • Please read this is source : https://stackoverflow.com/questions/44200720/difference-between-mock-mockbean-and-mockito-mock – Zaur May 24 '21 at 07:26
  • What version of Spring Boot are you using? Can you provide a copy of your build configuration (Maven `pom.xml` or Gradle `build.gradle`)? – Sam Brannen May 27 '21 at 14:23

1 Answers1

0

@MockBean is a feature of Spring Boot Test.

Thus you need to remove the @RunWith(MockitoJUnitRunner.class) declaration which is using Mockito's JUnit 4 Runner.

If you want to use JUnit 4, you must use the SpringRunner via @RunWith(SpringRunner.class).

If you want to use JUnit Jupiter (part of JUnit 5), you'll need to use the SpringExtension via @ExtendWith(SpringExtension.class).

Depending on the version of Spring Boot Test that you are using, you may be able to exclude the @ExtendWith(SpringExtension.class) declaration, since recent versions of @SpringBootTest automatically register the SpringExtension for you.

Related topic: Difference between @Mock, @MockBean and Mockito.mock()

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
  • added @Extendwith(SpringExtension.class) but still getting same output org.opentest4j.AssertionFailedError: expected: <{"id":1,"name":"Bob","tech":"java"}> but was: <{"id":0,"name":"Bob","tech":"java"}> – Bharath May 26 '21 at 12:09