无法使用 Mockito 和 Spring 模拟方法

Unable to mock a method with Mockito and Spring

我有一个 class JobDelegate,我正在使用 mockito 为其编写单元测试。我无法模拟 HTTPOperations class。我也尝试过使用来自测试 class 的 setter 注入。但它不起作用。下面是代码的最新修订版。我尝试使用 Power mock。但其中 none 有帮助。我无法预测哪里出了问题。

单元测试代码

 @ContextConfiguration(locations= "file:src/main/webapp/WEB-INF/spring- 
context.xml") 
 @RunWith(SpringJUnit4ClassRunner.class)
 //@RunWith(PowerMockRunner.class)
 /@PowerMockIgnore({ "javax.xml.*", "org.xml.*", "org.w3c.*" })
  //@PrepareForTest({ HTTPOperations.class})
  public class JobSubmissionDelegateTest{

private static Logger LOGGER = null;

private JobDelegate  jobDelegate ;  

private JobManager  jobImpl;    
@InjectMocks
private HTTPOperations operations;

//@Rule public MockitoRule mockitoRule = MockitoJUnit.rule();
@Before
public void setupTests() {
    jobDelegate = new JobDelegate();
    jobManager = new DBJobManagerImpl();
    operations = new HTTPOperations();
    jobManager.setHttpOperations(operations);
    jobSubmissionDelegate.setJobImpl(jobManager);
    //HTTPOperations httpOperationsSpy =spy(HTTPOperations.class);
    //doReturn("{\"response\":\"{\\"run_id\\":32423423}\n\"}").when(myClassSpy).method1();
    MockitoAnnotations.initMocks(this);

}
@Test
public void testExecuteJob() throws IOException {
//  PowerMockito.mockStatic(HTTPOperations.class);
    Mockito.when(operations.submitHttpPostRequest(any(), anyString())).thenReturn("{\"response\":\"{\\"run_id\\":32423423}\n\"}");
    //System.out.println("==>"+operations.submitHttpPostRequest(null, ""));
    ...........
    int runID = jobDelegate.executeJob(jobDetails);
    System.out.println("Run ID here " + runID);

}
}       


public class JobDelegate {
  // This is an interface.. and the implementation is passed from spring- 
   context.xml
  @Autowired
private JobManager jobImpl;

public int executeJob(JobDTO jobDto) {
............
return jobImpl.runBatchJob(jobDto);

}
}
public class DBJobManagerImpl implements JobManager{

@Autowired
private URIUtils uriUtils;

@Autowired
private HTTPOperations httpOperations;

@Override
public int runBatchJob(JobDTO jobDto) throws Exception {


    UriComponentsBuilder uri = uriUtils.createURI(ConfigUtil.getUrI());

    String response  = httpOperations.submitHttpPostRequest(uri, runSubmitJson);
    System.out.println("Response ==> " +response);
    .................
}
}

我能够使用 PowerMock 解决问题。 代码下方

 @RunWith(PowerMockRunner.class)
 @ContextConfiguration(locations= "file:src/main/webapp/WEB- 
 INF/Enrichment_Context.xml") 
 @PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
 @PowerMockIgnore({ "javax.xml.*", "org.xml.*", "org.w3c.*", 
 "javax.management.*" })
  @PrepareForTest({ HTTPOperations.class})
  public class JobDelegateTest {

@Autowired
private JobDelegate jobSubmissionDelegate;

@Test
public void testExecuteJob() throws IOException {
    PowerMockito.mockStatic(HTTPOperations.class);
    PowerMockito.when(HTTPOperations.submitHttpPostRequest(Mockito.any(), 
 Mockito.anyString())).thenReturn("{\"response\":\"{\\"run_id\\":32423423}\n\"}");
    ...................
    int runID = jobSubmissionDelegate.executeJobSubmission(jobDetails);
}
}