java - Testing a void method with Mockito -
i have method this:
public void somemethod() { a = new a(); // class. a.setvar1(20); a.somemethod(); }
i want test 2 things:
- the value of
var1
member variable of 20. somemethod()
called once.
i have 2 questions:
- are test objectives correct or should testing else ?
- how test using mockito ?
you can't test using mockito, because mockito can't access local variable code creates , let go out of scope.
you test method if injected dependency of class under test, or of method under test
public class myclass { private a; public myclass(a a) { this.a = a; } public void somemethod() { a.setvar1(20); a.somemethod(); } }
in case, create mock a, create instance of myclass mock a, call method , verify if mock has been called.
with code, is, way test code verify side effects of calling somemethod()
on var1 equal 20. if a.setvar1()
, a.somemethod()
don't have side-effect, code useless: creates object, modifies it, , forgets it.
Comments
Post a Comment