@Component public class MonitorManager { @Value("${monitor.url}") private String monitorUrl; @Value("${monitor.userName}") private String monitorUserName; // 其餘使用value的代碼省略 }
@RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(SpringRunner.class) @PowerMockIgnore({"javax.net.*","javax.management.*", "javax.security.*", "javax.crypto.*", "org.mockito.*"}) public class MonitorManagerTest { @InjectMocks private MonitorManager monitorManager; @Mock private OAuth2RestTemplate restTemplate; @Before public void setUp() throws Exception { ReflectionTestUtils.setField(monitorManager, "monitorUrl", "mock monitor url"); ReflectionTestUtils.setField(monitorManager, "monitorUserName", "mock userName"); } }