Running a single JUnit test in Eclipse

In the package explorer unfold the class. It should show you all methods. Right click on the one method you want to run, then select Run As -> JUnit from the context menu (just tested with Eclipse 3.4.1). Also selecting “Run” on a single entry in the JUnit-results view to re-run a test works in … Read more

Spring MVC Controllers Unit Test not calling @ControllerAdvice

After using the answer from @eugene-to and another similar one here I found limitations and raised an issue on Spring: https://jira.spring.io/browse/SPR-12751 As a result, Spring test introduced the ability to register @ControllerAdvice classes in the builder in 4.2. If you are using Spring Boot then you will need 1.3.0 or later. With this improvement, if … Read more

Setting up JUnit with IntelliJ IDEA [duplicate]

Create and setup a “tests” folder In the Project sidebar on the left, right-click your project and do New > Directory. Name it “test” or whatever you like. Right-click the folder and choose “Mark Directory As > Test Source Root”. Adding JUnit library Right-click your project and choose “Open Module Settings” or hit F4. (Alternatively, … Read more

JUnit testing with simulated user input

You can replace System.in with you own stream by calling System.setIn(InputStream in). InputStream can be a byte array: InputStream sysInBackup = System.in; // backup System.in to restore it later ByteArrayInputStream in = new ByteArrayInputStream(“My string”.getBytes()); System.setIn(in); // do your thing // optionally, reset System.in to its original System.setIn(sysInBackup); Different approach can be make this method … Read more

Why is assertEquals(double,double) deprecated in JUnit?

It’s deprecated because of the double’s precision problems. If you note, there’s another method assertEquals(double expected, double actual, double delta) which allows a delta precision loss. JavaDoc: Asserts that two doubles are equal to within a positive delta. If they are not, an AssertionError is thrown. If the expected value is infinity then the delta … Read more