我有DI链(DbConfig -> Repository -> Component -> RestController)
DBConfig中的数据源在Repository中是自动连接的。
应用程序类- CtnStatusCheckerApplication.class
数据源创建查找JNDI (" is. ot4 . sbl_ds ")。我正在模拟数据源并将其放在Spring JNDI树中。
这是我的单元测试(Groovy):
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = CtnStatusCheckerApplication.class)
class SiebelRepositoryTest {
@Autowired
SiebelRepository siebelRepository
@Autowired
@Qualifier("sblDataSource")
DataSource sblDataSource
@BeforeClass
public static void setUpClass() throws Exception{
DataSource ds = mock(DataSource.class)
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder()
builder.bind("eis.OT4.SBL_DS",ds)
builder.activate()
}
@Test
public void testContext(){
assert siebelRepository != null
assert sblDataSource != null
println Whitebox.getInternalState(siebelRepository,"sblDataSource") // prints "null" why?
}
}
1 -为什么sblDataSource在siebelRepository为空?在这种情况下,@SpringApplicationConfiguration应该用哪个类初始化?顶级对象还是每个单独的bean ?(如服务、组件、存储库等)
请试试这个:-
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = CtnStatusCheckerApplication)
@WebAppConfiguration
class SiebelRepositoryTest {