Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • TestNG: As testing framework. We decided to move from junit to testNG some years ago when junit seemed to be stuck
  • Mockito: To allow easy mock-based testing. 
  • Hamcrest: To allow to read the asserts in a human-understandable way.

Testing a spring-based service or standard Java class

Actually this is the situation that you very often will write unit tests. You created or extended a service and want to ensure that it is working. So this is what you want to do:

...

Code Block
public class ReflectionCacheTest extends AbstractLogSupportTestBase {

	/** Class under test. */
	@InjectMocks
	ReflectionCache cache;



	/** Tests for the {@link ReflectionCache#invokeMethod(Class, String, Class[], Object, Object[], Object)} method. */	
	public static class InvokeMethod extends ReflectionCacheTest {

		@Test
		public void normalUsage() {
			String testString = "I am a test";

			String result = (String) cache.invokeMethod(String.class, "toString", testString, null, null);

			assertThat(result, is(testString));
		}

		@Test
		public void multipleInvocationOnSameObject() {
			... test method content ...
		}

		@Test
		public void methodWithParameter() {
			... test method content ...
		}

		... more test methods ...

	}

}

...

  1. The class under test should be marked with @InjectMocks even if now @Mock no @Mock fields are going to be injected. This is because before every test method execution, this field will be reset.
  2. The inner class has to extend the outer test class. This is needed to access the in this example the cache field.