我需要为solr SearchComponent(SearchComponent.process())编写单元测试。 我该怎么办?我可以使用SolrTestCaseJ4还是其他方式? 我目前正在使用SolrTestCaseJ4 ...
我目前正在使用SolrTestCaseJ4 ...
public class SearchComponentTest extends SolrTestCaseJ4 {
@BeforeClass
public static void beforeClass() throws Exception {
initCore("collection1/conf/solrconfig.xml", "collection1/conf/schema.xml", "src/test/resources/");
}
@Test
public void testPrepare() throws IOException {
MySearchComponent component = new MySearchComponent();
List<SearchComponent> components = new ArrayList<>();
components.add(component);
SolrQueryRequest req;
ResponseBuilder rb;
req = req("q", "*:*");
rb = new ResponseBuilder(req, new SolrQueryResponse(), components);
component.process(rb);
}
}
和我的SearchComponent:
public class MySearchComponent extends SearchComponent {
public void process(ResponseBuilder rb) throws IOException {
....
SolrQueryRequest req = rb.req;
SolrQueryResponse rsp = rb.rsp;
SolrParams params = rb.req.getParams();
SchemaField keyField = rb.req.getCore().getLatestSchema().getUniqueKeyField();
String core = rb.req.getCore().getName();
SolrIndexSearcher searcher = req.getSearcher();
QueryCommand cmd = rb.createQueryCommand();
QueryResult result = new QueryResult();
searcher.search(result, cmd);
rb.setResult(result);
BasicResultContext ctx = new BasicResultContext(rb, rb.getResults().docList);
rsp.add("response", ctx);
...
}
...
}
但是我在这条线上有NullPointerException: QueryCommand cmd = rb.createQueryCommand();
谢谢您的帮助!