Spring testing - Pyosch/powertac-server GitHub Wiki

up

Unit testing without autowiring

Units that have minimal @Autowired annotations (they do not depend on injected dependencies, or the dependencies are only in the UUT - unit under test) can be tested easily, using JUnit4 conventions. A good example of a test that does not involve dependency injection is common/test/../TariffSpecificationTests. If one or two dependencies must be injected in the UUT, but the UUT does not depend on services that in turn must be injected, then simple injection using ReflectionTestUtils.setField() can be used to avoid dependence on Spring for dependency injection. A good example of this type of test is common/test/../RateTests.

Unit testing with autowiring

Many components depend on services that in turn depend on other services, or they depend on services that cannot be used directly because of dependency relationships. For example, any dependence of common or server-interface on accounting will not work, because accounting depends on both common and server-interface. In the code, these dependency cycles are broken using interfaces in either common or server-interface, and of course in the server it's not a problem, but it's a problem for unit testing.

In these cases, there are two tools that make testing relatively easy. The first is a Spring test-runner. You get it by annotating your test class with @RunWith(SpringJUnit4ClassRunner.class). The second is the Mockito package, which automatically implements interfaces and intercepts calls to it. See server-interface/test/../TariffSubscriptionTests for an example. The file server-interface/test/test-config.xml shows how to instantiate the mockito mocks for needed services.

Integration testing