Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot debug Mockito / JUnit code in Eclipse, works fine with just JUnit

I've got JUnit tests that run just fine. Added Mockito to my build and I try to put a breakpoint in my JUnit test that uses Mockito to mock out some of the public methods. When I try to run the debugger on the class, I get the error "unable to install breakpoint in XXX due to missing line number attributes. Modify compiler options to generate line number attributes." I checked my compiler and I generate line numbers is selected.

like image 510
Albert T. Wong Avatar asked Jul 01 '10 02:07

Albert T. Wong


People also ask

How do I debug a JUnit test in Eclipse?

To debug a JUnit test via Eclipse, simply place your breakpoints as expected, and then right click the test method and choose “Debug As” -> “JUnit Test”. The test will start and will pause when the breakpoint is hit.

Is JUnit and Mockito are same?

JUnit and Mockito can be primarily classified as "Testing Frameworks" tools. JUnit and Mockito are both open source tools. It seems that Mockito with 9.02K GitHub stars and 1.62K forks on GitHub has more adoption than JUnit with 7.53K GitHub stars and 2.8K GitHub forks.

Does JUnit include Mockito?

Mockito can also be used with other testing frameworks like JUnit and TestNG. JUnit framework is a Java framework that is also used for testing. Now, JUnit is used as a standard when there is a need to perform testing in Java.

How do I run a test case in debug mode in Eclipse?

With the test class open in the Eclipse editor, simply right click in the editor view and select Debug As > TestNG Test. Eclipse will stop at any breakpoints you set just like it would with any other local debug process.


1 Answers

The exception you're seeing is caused by attempting to debug dynamically generated empty mock methods created by the mock() function. From your question, it looks like you actually want to be using partial mocks instead of full mocks, where only some methods are mocked and the remaining calls are delegated to the real implementation.

To create partial mocks, you should be using the spy() method instead of the mock() method. So, use

MyClass myMock = spy(new MyClass());

instead of

 MyClass myMock = mock(MyClass.class);
like image 86
cleberz Avatar answered Oct 27 '22 04:10

cleberz