Reputation: 660
I'm trying to excute the following test:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DBDataManipulatorServiceTestContext.class, loader =
SpringApplicationContextLoader.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class UserInterfaceBeanTest {
@Autowired
private DBDataManipulatorService dbDataManipulatorService;
.....
@Test
public void testGetDistinctWorkPackageId() {
WorkPackage workPackage1 = new WorkPackage();
workPackage1.setWorkPackageId("TEST1");
workPackage1.setWorkPackageName("WP_1");
WorkPackage workPackage2 = new WorkPackage();
workPackage2.setWorkPackageId("TEST2");
workPackage2.setWorkPackageName("WP_2");
WorkPackage workPackage3 = new WorkPackage();
workPackage3.setWorkPackageId("TEST3");
workPackage3.setWorkPackageName("WP_3");
WorkPackage workPackage4 = new WorkPackage();
workPackage4.setWorkPackageId("TEST4");
workPackage4.setWorkPackageName("WP_4");
Aircraft aircraft1 = new Aircraft();
aircraft1.setAircraftId(50001);
aircraft1.setModel("100");
Aircraft aircraft2 = new Aircraft();
aircraft2.setAircraftId(50002);
aircraft2.setModel("100");
aircraft1.addOrUpdateWorkPackageToAircraft(workPackage1);
aircraft1.addOrUpdateWorkPackageToAircraft(workPackage3);
aircraft2.addOrUpdateWorkPackageToAircraft(workPackage2);
aircraft2.addOrUpdateWorkPackageToAircraft(workPackage3);
Map<Integer, Aircraft> aircraftsMap = new HashMap<Integer, Aircraft>();
aircraftsMap.put(50001, aircraft1);
aircraftsMap.put(50002, aircraft2);
UserInterfaceBean userInterfaceBean = new UserInterfaceBean();
userInterfaceBean.setModel("100");
aircraftsMap = dbDataManipulatorService.saveToDataBase(aircraftsMap);
List<String> workPackgesIds = userInterfaceBean.getDistinctWorkPackageId();
assertTrue(workPackgesIds.contains("TEST1"));
assertTrue(workPackgesIds.contains("TEST2"));
assertTrue(workPackgesIds.contains("TEST3"));
assertTrue(!workPackgesIds.contains("TEST4"));
}
But I get NullPointerException due to dbDataManipulatorService that is not correctly injected, my serviceContext is the following:
@ComponentScan
@EnableAutoConfiguration
@EnableBatchProcessing
@EnableJpaRepositories
public class DBDataManipulatorServiceContext {
.....
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setName("aircraft").setType(H2)
.build();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean lef = new
LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dataSource);
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan("com");
return lef;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter =
new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(true);
hibernateJpaVendorAdapter.setGenerateDdl(true);
hibernateJpaVendorAdapter.setDatabase(Database.H2);
return hibernateJpaVendorAdapter;
}
.....
Can anyone tell me where is the problem? and how to solve it.
EDIT
Here's my DBDataManipulatorServiceImpl:
@Service
@Transactional
public class DBDataManipulatorServiceImpl implements DBDataManipulatorService {
.....
}
And here's it's interface:
public interface DBDataManipulatorService {
public AircraftRepository getAircraftRepository();
public Aircraft getAircraftById(String id);
public Map<Integer, Aircraft> saveToDataBase(Map<Integer, Aircraft> aircrafts);
public MyJob findJobByJobId(String jobId);
public WorkPackage findWorkPackageByAircraftIdAndWorkPackageId
(int aircraftId, String workPackageId);
public Aircraft findAircraftByaircraftId(int aircraftId);
public List<Aircraft> findAllAircrafts();
public List<String> findAllAvailableAircraftsModels();
public List<Aircraft> findAllAircraftsByModel(String model);
public List<WorkPackage> getWorkPackagesByAircraftModel(String model);
}
Upvotes: 1
Views: 158
Reputation: 49915
You have a @ComponentScan
but you have not put the name of the package to scan from. The default behavior is to scan from the package where you have put the @ComponentScan
essentially the package of your DBDataManipulatorServiceContext
config class. I am guessing this is not where your DBDataManipulatorService
is, if so just specify a package name to your @ComponentScan
Upvotes: 1
Reputation: 768
You need to mark the DBDataManipulatorService class somehow. Use @service or @component annotation over class name. This will tell spring to manage the class and thus to inject it in your test.
Upvotes: 0
Reputation: 43087
The @Autowired
annotation by default means that a bean must have been found otherwise you would have gotten a bean not found exception.
There are a few things that can cause this:
The unit test is instantiated via new DBDataManipulatorServiceContext()
instead of being scanned as a Spring Bean, in this case @Autowired
is ignored.
There is some XML configuration that overrides the annotations configuration, and so the bean is first created with autowiring on, and then later is discarded by a bean defined in XML with no parameter provided for the missing property
Upvotes: 0