开发者

Integration Test for JUnit

开发者 https://www.devze.com 2023-03-11 21:23 出处:网络
I am very new to TDD in general so please forgive me if my question does not make lots of sense. After looking looking around for a bit, it seems that jUnit is capable of implement integration test.

I am very new to TDD in general so please forgive me if my question does not make lots of sense.

After looking looking around for a bit, it seems that jUnit is capable of implement integration test. I am hoping that the community can provide me some guidance on how to write integration test. Here is a simple overview of my design.

I have Main1, that accept a list of zip files. Main will extract the zip files, edit the conten开发者_运维技巧t of the pdf inside the zip files, and put the final pdf files into folder X. If the number of pdf reach a THRESHOLD, then Main2Processor (not a main class) will get invoked and zip all the pdf files, and also create a report text file with the same name as the newly create zip file.

If I run Main2, it also will kick off Main2Processor, which will zip the pdf file and create text file reports (even though the number of pdf in folder X did not reach a THRESHOLD).

How do I write integration test testing the correctness for my above design?


You're right; JUnit can be used to write tests that would be called integration tests. All you have to do is relax the rules regarding tests not touching external resources.

First, I would refactor the main() of your application to do as little as you can possibly make it do; there isn't a really good way to test the code in a main() function. Have it construct and run an object (that object can be the object containing main(), if you wish), passing that new object your list of ZIP files. That object can now be tested using JUnit by just instantiating it.

Now, you just have to architect the test to set up a constant test environment and then perform a repeatable test. Create, or clear out, a temp directory somewhere, then copy over some test ZIP files into that directory. Then, run your main processor.

To detect that the proper behavior occurs when the threshold is reached, you just test for the existence of a zip file (and/or its absence if the threshold isn't reached).


Do you really want an "integration test" (that term is overloaded beyond comprehension now so if you could state your end-goals that'd help) ? How about an acceptance test where you use this console/GUI app like a real user with specific input and check for the expected output?

JUnit is just a test runner and is oblivious of what the test actually does. So yes, you could use it to write any test. However it has been built for unit-testing and that will leak sometimes.. e.g. the fact that the test shuts down on the first error / the first assert that does not succeed. Usually coarse-tests like to push ahead and get a bunch of errors at the end.

If you want an integration test - you would have to redesign your app to be callable from tests (if it is not already so). Raise specific obstacles to writing this test and I can offer more specific advice.


I think that you should create some utility methods for your tests. For example running applicaiton, checking directory, clearing directory etc.

Then you will be able to implement test like the following:

@Test
public mytest1() {
    exec(Main1.class, "f1.zip", "f2.zip");
    Assert.assertTrue(getFileCount(OUTPUT_DIR) < THRESHOLD);
    // perform verification of your files etc...
}


First of all you might describe your above specification, but from a "test sequence" point of view. For example, test one would provide to Main1 a set of N pdf files, N being under the threshold. Then your test code, after Main1 returned, would check X folder content, as well as reports, to verify that your expectations are met.

JUnit itself just helps running test case, it does not really help writing the tests. And JUnit is "unit test" oriented (but you can use it as well for integration tests, despite some situations does not suit well; when a global setup is required for example, or when the test cases are expected to be run in a specific order...).

Some additional libraries can help greatly to interact easily with the rest of your code : dbunit, httpunit and so on.

0

精彩评论

暂无评论...
验证码 换一张
取 消