Skip to content

Instantly share code, notes, and snippets.

@awilmore
Created May 22, 2012 11:24
Show Gist options
  • Save awilmore/2768456 to your computer and use it in GitHub Desktop.
Save awilmore/2768456 to your computer and use it in GitHub Desktop.
Populating Private Autowired Fields via Spring ReflectionTestUtils
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;
@RunWith(MockitoJUnitRunner.class)
public class ReflectionTestUtilsExampleTest {
@Mock private SomeDependency mockSomeDependency;
private SomeSortOfClass someSortOfClass;
@Before
public void init() {
initialiseTestClass()
}
private void initialiseTestClass() {
someSortOfClass = new SomeSortOfClass();
ReflectionTestUtils.setField(abProfileInterceptor, "someDependency", mockSomeDependency);
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment