简体   繁体   中英

How to override the behavior of a failed JUnit test case

I need to write a JUnit test case for a never ending main() method. This main() method after processing some files sleeps for a minute and then resumes its execution. This process goes on and on and on.

I used the below code to write JUnit for it:

@Test (timeout = 10000)
public void testMainMethod()
{
   ClassName.main(null);
   assertEquals(true, true);
}

And with no surprise, my test case failed with java.lang.Exception: test timed out after 10000 milliseconds message. Even when the main() method works as expected, this test case is going to be failed with the timeout exception. How could i override the behavior of this failing test case so that in case of timeout exception it should show the result as "Succeeded" for this test case.

Edit The actual requirement is: I need to search the files from a particular location and, if found any, then move them to different folder. This search should be done every 30 minutes. For this i have used below code:

public class FaxProcessor {
public static void main(String[] args) {
        LOGGER.info("*** Starting Fax Server Processor ***");
        int poll_time = 1800000;
        LOGGER.info("Poll Time set to " + poll_time + " millisec");

        FaxProcessor faxProcessor = new FaxProcessor();

        while (true) {
            try {
                if(LOGGER.debugEnabled()){
                    LOGGER.debug("Starting new pass of fax processor");
                }
                faxProcessor.startProcessing();
            } catch (Exception e) {
                LOGGER.error("Processing Error", e);
            } finally {
                try {
                    // Wait for next loop
                    Thread.sleep(poll_time);
                } catch (InterruptedException e) {
                    LOGGER.error("Thread Exception", e);
                }
            }
        }
    }
    // startProcessing and other private methods here
}   

The biggest problem is, I have only one public method {main() method} and all others are private, so i cant write JUnit test case for them. Also, main() method is not returning anything, it only moves files from one folder to another, and in case it fails in moving files in one pass, it tries to do the same in next pass. So using JUnit test case, i only want to check whether an unexpected exception is coming during whole process or not.

If i will not specify timeout in JUnit, the test case is never gonna complete then. As soon as the timeout occurs, i want to check, whether the test case is completed due to Timeout exception or some other exception has come from inside main() method that haulted JUnit test case.

In case it's Timeout exception, this implies, everything inside my program, went in the right direction till the timeout occured, so the test case should SUCCEED. In all other case it should show FAILED as JUnit status.

You have artificially constrained your testing environment, with this statement: "I have only one public method {main() method} and all others are private" -- who says? If this is a requirement imposed by your teacher, then you should seriously consider getting out of the class.

1) Proper testing means testing a) that candidate files are recognized, b) that candidate files are moved, and c) the operation occurs periodically.

2) Factor your main routine into the infinite-loop control part and the worker part. Even better, make the wait interval be a computed value.

3) Use protected methods instead of private methods. Since they are protected, you can now use a sub-class to get access to methods as you need, without violating necessary encapsulation.

You should think of separating long running unit tests. How to achieve that is for example shown here or here .

I don't understand what you want to do. If you got Timeout Exception, that means the test case ran too long.

If you want to test the main method, and it contains an infinite loop, how can you determine that the method works as expected?

For your problem, use

@Test (timeout = 10000, expected=Exception.class)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM