java - Jmockit: Unable to mock inner field. Always ends up being null -


i have trouble controlling going on variables inside methods test. in example, input method being tested mocked or injectable object yet if try it, null* exceptions.

class , method being tested:

public class someclass {     public someclass() {}      public void mymethod(inputclass input) {         //how set field no matter what, don't exeption?         long somelong = long.parselong(input.getsomelong());          // how prevent numberformatexception when long.parselong() called in situation?         someinnerclass innerclass = someinnerclass(long.parselong(input.getsomelong()));         // how prevent numberformatexception when long.parselong() called in situation?         innerclass.dostuff(long.parselong(input.getsomelong()));          // ...     } } 

test class:

public class testsomeclass {     @tested private someclass classbeingtested;     @injectable someinnerclass innerclass;     @injectable inputclass input; // doesn't matter if use @mocked here      @test     public void shouldtestsomething() {         new nonstrictexpectations() {{             // expectations innerclass...         }};               classbeingtested.mymethod(input); // @ point error long.parslong()           // ...      } } 

i want able ignore errors in parts of code within method testing. don't care @ long.parselong() error in specific case. i'm not trying test that, yet getting in way of real test. how create fake object, in place of object causing issues, can ignored while test other part of method?

please use below code , check if help. need add dependency mockito project.one important think make sure use @runwith(mockitojunitrunner.class) in test class.

@runwith(mockitojunitrunner.class) public class testsomeclass {     @tested private someclass classbeingtested;     @injectable someinnerclass innerclass;     @mock     inputclass input; // doesn't matter if use @mocked here      @test     public void shouldtestsomething() {         new nonstrictexpectations() {{             // expectations innerclass...         }};              mockito.when(input).getsomelong().thenreturn("11").thenreturn("11");//this return 11 in place of input.getsomelong()for 2 times         classbeingtested.mymethod(input); // in case yoou not error long.parslong()           // ...      } } 

=======================================================

using jmockit :

 import org.junit.test;  import mockit.injectable; import mockit.mocked; import mockit.nonstrictexpectations; import mockit.tested;  public class someclasstest {     @tested     private someclass classbeingtested;     @injectable     someinnerclass innerclass;      @mocked     inputclass input;      @test     public void shouldtestsomething() {         new nonstrictexpectations() {              {                 input.getsomelong();                 returns("11");             }         };          classbeingtested.mymethod(input);           // ...     } } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -