English 中文(简体)
你们能否在重新测试的班子中打断方法?
原标题:Can you mock out method calls in the class you re testing?

我试图为我的代码撰写Junnit测试,但在我的一些方法中,我叫作其他方法。 是否可能改变这些呼吁?

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

书写法是我想要弄错的,是我所测试的那类方法的一部分,但我不知道如何加以.弄。 我认为,打碎I米级测试,然后加上对我的期望的号召,会这样做,但这种方法仍然使用。

没有人会向我提出一些建议,说明在这方面应做些什么?

EDIT:

我的约会法典认为:

@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
最佳回答

PowerMock让你部分参加模拟课程,但是为了方便的Mock而不是JMock。 无论如何,这不是最佳办法。

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);
}; 

在您的测试守则中,在受测试的类别中(在一组左右或使外地得到保护)超过<代码>(FileWriter。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签