I have the following problem in my unit test. The class with tests:
class TestClass {
    boolean stopTest = false;
    @Test
    public void test() {
        // do something
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                stopTest = true;
            }
        }, 1200);
        while(!stopTest) {
        }
        timer.cancel();
        // do assert
    }
}
This test works right only in debug with breakpoint on while operator, but if I run test not in debug or without breakpoint, he works infinitely. I tried to change stopTest to class with boolean field, also a tried to work through get and set methods in this class. What I do wrong?
 
     
     
     
    