【问题标题】:Springboot test issue: Mockito is returning empty objectSpringboot 测试问题:Mockito 正在返回空对象
【发布时间】:2019-07-11 19:35:20
【问题描述】:

我正在尝试构建一个使用 gradle 作为构建工具和 openjdk-11 的原型。这个原型将在springboot框架上构建一个rest-api。

我的模块可以正常使用 rest-api 调用并返回预期结果。但是,当我现在尝试为其余 api 编写测试时,测试失败,因为 Mockito 正在返回空对象。对于我应该如何为这个 rest-api 编写测试或如何修复它的任何见解,我将不胜感激。

我的控制器:

@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));
}
}

服务:

@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);
}
}

模特:

@Builder
@Data
@RequiredArgsConstructor
@JsonDeserialize(builder = Greeting.class)
public class Greeting {
    @NonNull
    private Long id;

    @NonNull
    private String content;

}

主类:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我是这样执行的:

gradle bootrun

然后从浏览器中尝试:

http://localhost:8080/greeting 

然后返回:

{"id":0,"content":"Hello, World!"}

再次尝试:

http://localhost:8080/greeting?name=Patty

然后返回:

{"id":0,"content":"Hello, Patty!"}

现在,我正在尝试编写测试来以编程方式验证类似于上述调用的 api 调用。所以我尝试了:

@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)


    }




}

错误信息是:

org.junit.ComparisonFailure: 
Expected :"{"id":0,"content":"Hello, Patty!"}"
Actual   :""

从这行失败:

assertThat(response.getContentAsString()).isEqualTo(expectedResponse)

提前致谢。

【问题讨论】:

  • 您知道您的测试需要一个正在运行的服务器吗?您只能以这种方式针对正在运行的应用程序进行测试。

标签: spring spring-boot spring-mvc mockito


【解决方案1】:

这有助于我理解:Mockito - thenReturn always returns null object

我将 Mockito.when 部分更改为:

Mockito.when(service.getGreetings(Mockito.anyLong(),Mockito.anyString())).thenReturn(greeting);

成功了

【讨论】:

    猜你喜欢
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 2013-10-16
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多