I need to create tests for various functions, which are not static (which I'm not allowed to change). E.g.
public Double average(int[] scores) {
int sum = Arrays.stream(scores).sum();
return (double) sum/scores.length;
}
Test:
public void testAverage (){
int[] scores = {25, 100, 90, 72};
Double expected = 59.25;
Double result = LogicProblems.average(scores);
assertEquals(expected, result);
}
interface:
public Double average(int[] scores);
Doing so, I get the error message "can't call a non-static method from static context"
Could you tell me why the context is static, and how to work around/with it?
LogicProblemsclass, you need to use an instance ofLogicProblems. That's what not being static means. Depending on how the class is written it might be as simple asnew LogicProblems().average(scores)