您可以在您正在测试的类中模拟方法调用吗?

发布于 2024-12-25 21:25:42 字数 1789 浏览 2 评论 0原文

我正在尝试为我的代码编写 JUnit 测试,但在我的某些方法中调用了其他方法。是否可以模拟这些调用?

例如,

s3FileWrite(File file, Status status)
{
  S3 s3         = new S3(file.getName, s3Service)
  String key    = s3.getKey();
  String bucket = s3.getBucket();
  File tmp = new File("tmp/" + s3.getName());

  writeFile(key, bucket, tmp, status); //local method call I want to mock out
}//awsFileWrite

writeFile 方法是我想要模拟的方法,它是我正在测试的类的一部分,但我不知道如何模拟它。我认为模拟我正在测试的类,然后将调用添加到我的期望中就可以了,但它仍然调用该方法。

有人可以给我一些关于在这里做什么的建议吗?

编辑:

我的 JMock 代码如下所示:

@Test
public void testS3FileWrite()
{       
    fileName     = context.mock(File.class);
    s3Service    = context.mock(FileDataAccessor.class);
    s3           = context.mock(S3.class);          
    reportWriter = context.mock(ReportWriter.class); 

    try
    {
        context.checking(new Expectations(){{           
            oneOf(fileMetaData).getKey();
            will(returnValue("s3Key")); 

            oneOf(fileMetaData).getBucketName();
            will(returnValue("BucketName"));

            oneOf(fileMetaData).getName();
            will(returnValue("TempFile"));  

            ((MethodClause) oneOf (any(File.class))).method("File").with(same("tmp/TempFile")); 

            oneOf(reportWriter).writeFile(with(same("s3Key")),
                                   with(same("BucketName")), 
                                   with(any(File.class), 
                                   with(same(Status.OK)));
        }});//Expectations
    }
    catch (Exception e)
    {
        ErrorStatus.debug("Exception in ReportTest.testS3FileWrite: " + e);
    }//try-catch  

    ReportWriter test = new ReportWriter(status);
    test.awsFileWrite(fileName, Status.OK);
}//testAWSFileWrite

I'm trying to write JUnit tests for my code but with in some of my methods other methods are called. Is it possible to mock these calls out?

E.g.

s3FileWrite(File file, Status status)
{
  S3 s3         = new S3(file.getName, s3Service)
  String key    = s3.getKey();
  String bucket = s3.getBucket();
  File tmp = new File("tmp/" + s3.getName());

  writeFile(key, bucket, tmp, status); //local method call I want to mock out
}//awsFileWrite

The writeFile method is what I want to mock out and it's part of the class I am testing, but I don't know how to mock it out. I thought mocking out the class I'm testing and then adding the call to my expectations would do it but it still calls the method.

Can anyone give me some advice on what to do here please?

EDIT:

My JMock code looks like this:

@Test
public void testS3FileWrite()
{       
    fileName     = context.mock(File.class);
    s3Service    = context.mock(FileDataAccessor.class);
    s3           = context.mock(S3.class);          
    reportWriter = context.mock(ReportWriter.class); 

    try
    {
        context.checking(new Expectations(){{           
            oneOf(fileMetaData).getKey();
            will(returnValue("s3Key")); 

            oneOf(fileMetaData).getBucketName();
            will(returnValue("BucketName"));

            oneOf(fileMetaData).getName();
            will(returnValue("TempFile"));  

            ((MethodClause) oneOf (any(File.class))).method("File").with(same("tmp/TempFile")); 

            oneOf(reportWriter).writeFile(with(same("s3Key")),
                                   with(same("BucketName")), 
                                   with(any(File.class), 
                                   with(same(Status.OK)));
        }});//Expectations
    }
    catch (Exception e)
    {
        ErrorStatus.debug("Exception in ReportTest.testS3FileWrite: " + e);
    }//try-catch  

    ReportWriter test = new ReportWriter(status);
    test.awsFileWrite(fileName, Status.OK);
}//testAWSFileWrite

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2025-01-01 21:25:42

PowerMock 允许您部分模拟类,但它是为 EasyMock 而不是 JMock 设计的。无论如何,这都不是最好的方法。

添加一个新类 FileWriter 并将 writeFile 方法移至其中,然后在您的测试类中,
委托给一个:

// default implementation, can be replaced in tests
FileWriter fileWriter = new FileWriter();

writeFile(key, bucket, tmp, status) {
    fileWriter.write(key, bucket, tmp, status);
}; 

在您的测试代码中,使用模拟 FileWriter 覆盖被测类中的 fileWriter 字段(添加 setter 或使该字段受保护)。

PowerMock lets you partially mock classes, but it's designed for EasyMock not JMock. In any case, this is not the best approach.

Add a new class FileWriter and move the writeFile method to it, then in your class under test,
delegate to one:

// default implementation, can be replaced in tests
FileWriter fileWriter = new FileWriter();

writeFile(key, bucket, tmp, status) {
    fileWriter.write(key, bucket, tmp, status);
}; 

In your test code, overwrite the fileWriter field in a the class under test (add a setter or make the field protected) with a mock FileWriter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文