Sigma
Sigma

Reputation: 1

Is there a way converting String to SolrDocumentlist?

I am trying to write MockMVC test for a searchController where I want to mock entry point of solr data to the api. So that my api do the rest of the sorting stuff and through mockMVC test I can validate the service.

@ExtendWith({SpringExtension.class})
@WebMvcTest(SeaController.class)
@AutoConfigureMockMvc(addFilters = false)
class SeaControllerTest {

    private MockMvc mockMvc;
    
    @Autowired
    WebApplicationContext webApplicationContext;

    @MockBean
    private SeaService seaService;

    @MockBean
    MesService mesService;

    @MockBean
    private SeaCommand seaCommand;

    @MockBean
    private AuthenticationProvider authenticationProvider;
    
    private SolrDocumentList mockedSolrDocList ;
    
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

    }

    @Test
    public void findByQuery() throws Exception {
        String jsonBody = (Files.readString(Paths.get("local path to /payload.txt")));
        String responseBody = (Files.readString(Paths.get("local path to /response.txt")));
        
        //solrDoc contains list of SolrDocs 
        String mockSolrdocList = (Files.readString(Paths.get("local path to /solrDoc.txt")));
        
        //getting null pointer exception for below line
        when(mockedSolrDocList.toString()).thenReturn(mockSolrdocList);

        mockMvc.perform(MockMvcRequestBuilders.post("/query")
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                        .characterEncoding("utf-8")
                        .content(jsonBody)
                        .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().string(org.hamcrest.Matchers.containsString(responseBody)));
    }

}

Upvotes: 0

Views: 98

Answers (1)

xerx593
xerx593

Reputation: 13289

You "forgot" to mock mockedSolrDocList! ;)

Or (annotate):

@Mock
private SolrDocumentList mockedSolrDocList;

Or (initialize):

//...
mockedSolrDocList = /*Mockito.*/mock(SolrDocumentList.class);

Both will:

  • create a mock object. (which is eligible for when, verify,...)
  • eliminate the NPE.

Upvotes: 0

Related Questions