### Question:
MetalSearcher extends Searcher { @Override public Result search(Query query, Execution execution) { QueryTree tree = query.getModel().getQueryTree(); if (isMetalQuery(tree)) { OrItem orItem = new OrItem(); orItem.addItem(tree.getRoot()); orItem.addItem(new WordItem("metal", "album")); tree.setRoot(orItem); query.trace("Metal added", true, 2); } return execution.search(query); } @Inject  MetalSearcher(MetalNamesConfig config);  MetalSearcher(); @Override Result search(Query query, Execution execution);  }### Answer:
@Test void testAddedOrTerm1() { MetalNamesConfig.Builder builder = new MetalNamesConfig.Builder(); builder.metalWords(Arrays.asList("hetfield", "metallica", "pantera")); MetalNamesConfig config = new MetalNamesConfig(builder); Chain<Searcher> myChain = new Chain<>(new MinimalQueryInserter(), new MetalSearcher(config)); Execution.Context context = Execution.Context.createContextStub(); Execution execution = new Execution(myChain, context); Result result = execution.search(metalQuery); System.out.println(result.getContext(false).getTrace()); assertAddedOrTerm(metalQuery.getModel().getQueryTree().getRoot()); }
@Test void testAddedOrTerm2() { try (Application app = Application.fromApplicationPackage( FileSystems.getDefault().getPath("src/main/application"), Networking.disable)) { Search search = app.getJDisc("default").search(); Result result = search.process(ComponentSpecification.fromString("metalchain"), metalQuery); System.out.println(result.getContext(false).getTrace()); assertAddedOrTerm(metalQuery.getModel().getQueryTree().getRoot()); } }
@Test void testWithMockBackendProducingHits() { DocumentSourceSearcher docSource = new DocumentSourceSearcher(); Query testQuery = new Query(); testQuery.setTraceLevel(6); testQuery.getModel().getQueryTree().setRoot(new WordItem("drum","album")); Result mockResult = new Result(testQuery); mockResult.hits().add(new Hit("hit:1", 0.9)); mockResult.hits().add(new Hit("hit:2", 0.8)); docSource.addResult(testQuery, mockResult); Chain<Searcher> myChain = new Chain<>(new MetalSearcher(), docSource); Execution.Context context = Execution.Context.createContextStub(); Execution execution = new Execution(myChain, context); Result result = execution.search(testQuery); System.out.println(result.getContext(false).getTrace()); assertEquals(2, result.hits().size()); }