简体   繁体   English

Jackson ObjectMapper 在 JUnit 5 控制器测试中为空

[英]Jackson ObjectMapper is null in JUnit 5 Controller Test

I have tried with @Autowired on the objectMapper, also tried to Mock it but no success, I just want to use the writeValueAsStringMethod so I do not have to pass a long json string to the content method below.我已经尝试在 objectMapper 上使用@Autowired,也尝试过模拟它但没有成功,我只想使用 writeValueAsStringMethod,所以我不必将长 json 字符串传递给下面的内容方法。

If I mark my class with @SpringBootTest and also @AutoconfigureMockMvc it works (the objectmapper is not null) but I believe that there must be another way so that it does not become mandatory to use this annotations.如果我用@SpringBootTest@AutoconfigureMockMvc标记我的类,它可以工作(objectmapper 不为空)但我相信必须有另一种方法,这样它就不会成为强制使用这个注释。

TestClass:测试类:

@ExtendWith(MockitoExtension.class)
public class CarControllerTest {

    private MockMvc mockMvc;

    @InjectMocks
    private CarController carController;

    @Mock
    private ObjectMapper objectMapper;

    @MockBean
    private CarParts carParts;

    @BeforeEach
    public void before() {
        mockMvc = MockMvcBuilders.standaloneSetup(carController).build();
    }

    @Test
    @DisplayName("Car Controller Test")
    public void carControllerTest() {

        try {
            CarCustomRequest carCustomRequest = buildRequest();
            ResultActions resultActions = mockMvc.perform(post("/custom/endpoint")
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(objectMapper.writeValueAsString(carCustomRequest)));
            MvcResult mvcResult = resultActions.andExpect(status().isOk()).andReturn();
            assertTrue(mvcResult.getResponse().getStatus() == 200);
        } catch (Exception e) {
            fail("Error testing /custom/endpoint");
        }
    }

In order to @Autowire you need to load component classes that create the corresponding bean.为了@Autowire ,您需要加载创建相应bean的组件类。 To make tests faster you could define custom application context and load required beans only inter of using @SpringBootTest without params.为了使测试更快,您可以定义自定义应用程序上下文并仅在使用不带参数的@SpringBootTest之间加载所需的 bean。

@SpringBootTest(classes = JacksonAutoConfiguration.class)
class ObjectMapperTest {

    @Autowired
    private ObjectMapper mapper;

    @Test
    void checkObjectMapper() {
        assertNotNull(mapper);
    }
}

I would not use @Mock in this case because it will be required to create stubs for required methods.在这种情况下我不会使用@Mock ,因为它需要为所需的方法创建存根。

As an alternative, you could simply create a new instance of the ObjectMapper in test.作为替代方案,您可以简单地在测试中创建ObjectMapper的新实例。

class ObjectMapperTest {
    private ObjectMapper mapper = new ObjectMapper();

    @Test
    void checkObjectMapper() {
        assertNotNull(mapper);
    }
}

You could also register additional modules if required如果需要,您还可以注册其他模块

objectMapper.registerModule(new JavaTimeModule());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM