慕虎7371278
123456789101112131415161718192021222324252627282930313233package cn.outofmemory.junit.jmockit; import mockit.NonStrictExpectations;import mockit.Injectable;import mockit.MockUp;import mockit.Mocked;import mockit.Mockit;import mockit.NonStrict;import mockit.Tested; public class MockTargetTest { @BeforeClass static public void beforeClass() { Mockit.setUpMocks(); } @Mocked private MockTarget mt; @Test public void testPrivateMethodMock() { new NonStrictExpectations(mt) { { // 对私有方法进行mock this.invoke(mt, "privateMethod", anyString); result = "Hello Object"; } }; Object actual = mt.publicMethod(); assertEquals(actual,"Hello Object"); }}