1

If I write a test class such as:

public class TryOut {

    public int doStuff()
    {
        System.out.println("Hello");
        int five = 5;
        return five * 2;
    }

    public static void main(String[] args)
    {
        TryOut myTryOut = new TryOut();
        myTryOut.doStuff();
    }
}

and use JUnit to test it

import static org.junit.Assert.*;

import org.junit.Test;

public class TryOutTest {

    @Test
    public void test() {

        TryOut myTryOut = new TryOut();
        int answer = myTryOut.doStuff();
        assertEquals(10, answer);
    }

}

What I want is to step through the code when JUnit is running. Is this possible? If I set a breakpoint on the method that gets called - doStuff() and then right click on the TryOutTest class, then Debug As, JUnit test menu options, (to run the code) the breakpoint never stops the code, and the test completes.

3
  • That should work out of the box. Try putting the breakpoint on the line before within your test.
    – GhostCat
    Commented May 21, 2017 at 7:39
  • Of course debug should work for you. Check that your breakpoint is not disabled and option disabling all breakpoints is not enabled as well Commented May 21, 2017 at 7:41
  • why did you debug in unit-tests? maybe you need make the test smaller into a couple of tests, see the section Test Isolation section would be helped you.
    – holi-java
    Commented May 21, 2017 at 7:48

1 Answer 1

2

Make sure that your breakpoint is not skipped (in case you see a backslash on the breakpoint, it is skipped). In case it is skipped, press Ctrl+Alt+B to enable it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.