[英]Why is my second method's mock affecting the first method?
我寫的 class 有兩種方法,第二種方法包含強制異常模擬。 當我分別運行測試時,它們都通過了,但是當它們一起運行時,在第一種方法中調用了強制異常。 我試過 tearDown 但這種方法不可用。
public class LambdaHandlerTest {
@Test
@DisplayName("Testing the handleRequest")
void testTheHandleRequest() throws URISyntaxException, IOException {
new MockUp<ProviderEvents>(){
@Mock
public File fetchFile(String bucket, String jobName, Context context) throws IOException{
File file = new File ("src/test/resources/testEmailFile.txt");
return file;
}
};
new MockUp<AbstractAmazonSimpleEmailService>(){
@Mock
SendEmailResult sendEmail(SendEmailRequest var1){
return null;
}
};
LambdaHandler lambdaHandler = new LambdaHandler();
assertEquals ("Finished handleRequest()", lambdaHandler.handleRequest(generateS3Event(), null));
}
@Test
@DisplayName("Test catch block of handleRequest")
void testCatchBlockHandleRequest() throws IOException, URISyntaxException {
LambdaHandler lambdaHandler = new LambdaHandler();
S3Event s3Event = generateS3Event();
new MockUp<ServiceEvents>() {
@Mock
public Boolean extractJson(S3Event event, Context context) throws Exception {
throw new Exception("Forced Exception");
}
};
assertEquals("Error with handleRequest()", lambdaHandler.handleRequest(s3Event, null));
}
public S3Event generateS3Event() throws URISyntaxException, IOException {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(Objects.requireNonNull(classLoader.getResource("s3Event.json")).toURI());
S3Event s3Event = new ObjectMapper().readValue(file, S3Event.class);
return s3Event;
}
}
任何幫助,將不勝感激。
您可以將第二個方法用於第一個方法並檢查兩個斷言語句是否都執行,而不是使用兩種方法
我不確定為什么會發生這種情況,但最后我使用@order 確保使用拋出強制異常的模擬的測試最后運行。 成功了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.