mercredi 10 juin 2015

Can I inject primitive variable into mocked class using annotation?

For example I have handler:

@Component
public class MyHandler {

  @AutoWired
  private MyDependency myDependency;

  @Value("${some.count}")
  private int someCount;

  public int someMethod(){
    if (someCount > 2) {
    ...
  }
}

to test it I wrote the following test:

@RunWith(MockitoJUnitRunner.class}
class MyHandlerTest {

  @InjectMocks
  MyHandler myHandler;

  @Mock
  MyDependency myDependency;

  @Test
  public void testSomeMethod(){
    ReflectionTestUtils.setField(myHandler, "someCount", 4);
    myHandler.someMethod();
  }
}

I can mock variable someCount using ReflectionTestUtils. Can I somehow mock it using Mockito annotation?

Aucun commentaire:

Enregistrer un commentaire