java测试类调用方法_java – 你可以在你正在测试的类中模拟方法调用吗?

我正在尝试为我的代码编写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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值