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:

  1. the value of var1 member variable of 20.
  2. somemethod() called once.

i have 2 questions:

  1. are test objectives correct or should testing else ?
  2. 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

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -