How-to articles, tricks, and solutions about JUNIT
To test a class that has private methods, fields, or inner classes, you can do the following:
To assert that a certain exception is thrown in a JUnit test, you can use the @Test annotation's expected attribute. Here's an example:
In JUnit, you can use the getClass().getClassLoader().getResource("").getPath() method to get the path of the src/test/resources directory.
To mock a final class with Mockito, you can use the PowerMockito library.
In JUnit 4, you can use the @FixMethodOrder annotation to specify the order in which test methods should be executed.
To test that no exception is thrown in a Java method, you can use the assertDoesNotThrow method from the org.junit.jupiter.api.Assertions class (part of the JUnit 5 library).
To verify that a specific method was not called using Mockito, you can use the verifyZeroInteractions method.
To read a text file in Java, you can use the BufferedReader class from the java.io package.
To verify that a method was called on an object created within a method using Mockito, you can use the Mockito.verify() method and pass it the object that you want to verify, as well as the method that you want to verify was called.
To mock and assert a thrown exception in Mockito, you can use the doThrow() method and the verify() method.
It is generally considered bad practice to test private methods, as they are an implementation detail that should not be exposed to the outside world.