I would like to test the endpoint if the response and HTTP code are correct. The controller method looks like this:
@CrossOrigin
@RequestMapping(method = RequestMethod.GET, value = "/{ruleId}")
public Rule loadOneRule(@PathVariable String ruleId) {
return rulesService.loadOneRule(ruleId);
}
The test method is
@Test
public void loadOneRule() throws IOException, URISyntaxException {
NodeDTO nodeDto = new NodeDTO();
HashMap<String, NodeDTO> nodes = new HashMap<>();
nodes.put("foo", nodeDto);
Rule rule = new Rule("my rule", nodes);
RuleService ruleService = new RuleService();
rule = ruleService.saveRule(rule);
String id = rule.getId().toString();
String target = "http://localhost:8090" + "/v2/rules/" + id;
URI uri = new URI(target);
HttpGet httpGet = new HttpGet(uri.toASCIIString());
HttpResponse response = httpClient.execute(httpGet);
int HTTPcode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String json = EntityUtils.toString(entity);
ObjectMapper objectMapper = new ObjectMapper();
Rule targetRule = objectMapper.readValue(json, Rule.class);
boolean correctStatus = HTTPcode >= 200 && HTTPcode <= 300 ? true : false;
boolean correctResponse = targetRule != null ? true : false;
assertTrue(correctStatus);
assertTrue(correctResponse);
}
I get nullpointer exception on my ruleService. It is the same even if I try to @Autowire it and not instantiate it. I guess the whole approach about getting one rule object from the mongo database is wrong, but putting an object locally in my database and getting this object by his id would be even worse, since these tests will not run on my computer
Aucun commentaire:
Enregistrer un commentaire