@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {TestConfig.class},
loader = AnnotationConfigContextLoader.class)
@ActiveProfiles("test")
public abstract class AbstractTest {
}
@Configuration
@ComponentScan("com.cleverlance.academia.spring.test")
public class TestConfig {
}
public class CustomerRepositoryTest extends AbstractTest {
@Autowired
private CustomerRepository customerRepository;
@Test
public void findById_shouldReturnCorrectCustomer() {
customerRepository.fin....
}
}
public class CustomerServiceTest extends AbstractTest {
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@InjectMocks
@Autowired
private CustomerService customerService;
@Mock
private CustomerRepository customerRepository;
@Configuration
@ComponentScan("com.cleverlance.academia.spring.test")
public class TestConfig {
@Bean
@Profile("test")
public Database database() {
return new DatabaseTest();
}
}
@Configuration
@ComponentScan("com.cleverlance.academia.spring.test")
public class AppConfig {
@Bean
@Profile("!test")
public Database database() {
return new DatabaseImpl();
}
}