Issue
I am trying to build a prototype that is using gradle as build tool and openjdk-11. This prototype will build a rest-api on springboot framework.
My module is working fine with rest-api call and returning expected result. However, as I am now trying to write a test for the rest api, the test is failing as Mockito is returning empty object. Would appreciate any insight on how should I write a test for this rest-api or what to do to fix it.
My controller:
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@Autowired
GreetingService service;
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return service.getGreetings(0L, String.format(template, name));
}
}
The service:
@Service
public class GreetingService {
public Greeting getGreetings() {
return new Greeting(1L, "Hello World");
}
public Greeting getGreetings(Long id, String name) {
return new Greeting(id, name);
}
}
The Model:
@Builder
@Data
@RequiredArgsConstructor
@JsonDeserialize(builder = Greeting.class)
public class Greeting {
@NonNull
private Long id;
@NonNull
private String content;
}
The main class:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I executed this by :
gradle bootrun
And then from browser, tried :
http://localhost:8080/greeting
And that returned:
{"id":0,"content":"Hello, World!"}
Again tried:
http://localhost:8080/greeting?name=Patty
and that returned:
{"id":0,"content":"Hello, Patty!"}
Now, I was trying to write test to validate api calls similar to the above calls programatically. So I tried:
@RunWith(MockitoJUnitRunner.class)
public class GreetingControllerTest {
private MockMvc mockMvc;
@Mock
private GreetingService service;
@InjectMocks
private GreetingController controller
@Test
public void testGreeting() throws Exception {
Greeting greeting = new Greeting(0L,"Patty!");
String expectedResponse = "{\"id\":0,\"content\":\"Hello, Patty!\"}";
//JacksonTester.initFields(this, new ObjectMapper());
mockMvc = MockMvcBuilders.standaloneSetup(controller)
.build();
Mockito.when(service.getGreetings(0L,"Patty")).thenReturn(greeting);
MockHttpServletResponse response = mockMvc
.perform(get("/greeting?name=Patty")
.contentType(MediaType.ALL))
.andReturn()
.getResponse();
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
assertThat(response.getContentAsString()).isEqualTo(expectedResponse)
}
}
The error msg is :
org.junit.ComparisonFailure:
Expected :"{"id":0,"content":"Hello, Patty!"}"
Actual :""
Failing from this line:
assertThat(response.getContentAsString()).isEqualTo(expectedResponse)
Thanks in advance.
Solution
This helped me understanding: Mockito - thenReturn always returns null object
I changed the Mockito.when part to :
Mockito.when(service.getGreetings(Mockito.anyLong(),Mockito.anyString())).thenReturn(greeting);
and it worked
Answered By - VictorGram