具有多个bean依赖项的测试方法

时间:2019-06-19 20:10:11

标签: java spring spring-boot annotations

在测试中,我只需要测试方法insertData

@Component
public class DatabaseServiceImpl implements DatabaseService {

    @Autowired
    DataSource dataSource;

    @Override
    public int insertData(String var1, int var2, String var3, String var4, int var5, int var6) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return jdbcTemplate.update("INSERT INTO table_name_here VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", 
                var1, var2, var3, var4, var5, var6,
                null, null, null);
    }
}

它使用dataSource,它是在java src的config类中的Bean中定义的,因此对于我的测试,我需要使用EmbeddedDataSource,所以我这样编写测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DatabaseServiceImplTestConfig.class, loader=AnnotationConfigContextLoader.class)
public class DatabaseServiceImplTest {

    @Autowired
    @Qualifier("embedded")
    DataSource dataSource;

    @Test
        public void testInsertData() {
            ...........
            DatabaseServiceImpl dsi = new DatabaseServiceImpl();
            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
            assertEquals(1, dsi.insertData(var1, var2, var3, var4, var5, var6));
            ...........
        }

当我调试它时,它说我正在使用EmbeddedDataSource,但是JUint会抛出此异常:

java.lang.IllegalArgumentException: Property 'dataSource' is required

并指向此assertEquals行。如何防止它从src包中要求DataSource,以便可以使用此嵌入式代码完成测试?暂停

编辑

这是测试配置,我在其中指定需要使用的数据源(甚至设置名称):

@Bean(name="embedded")
public DataSource embeddedDataSource(DataSourceFactory embeddedDataSourceFactory) {

与src config类相同:

@Bean
public Datasource dataSource()

stackTrace是:

java.lang.IllegalArgumentException: Property 'dataSource' is required
    at org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:135)
    at org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:168)
    at ......DatabaseServiceImpl.insertData(DatabaseServiceImpl.java:18)
    at ......DatabaseServiceImplTest.testInsertData(DatabaseServiceImplTest.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

但是我知道它指出了insertData方法内部的dataSource

1 个答案:

答案 0 :(得分:2)

因为您在测试中正在执行new,所以您失去了其他依赖项。对您的测试进行一些如下所示的重构将有助于修复。


@Autowired
private DatabaseService databaseService;

@Test
public void testInsertData() {
    ...........
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
     assertEquals(1, databaseService.insertData(datetime, days_rolling, host, start_or_stop, scenario_threadpool_size, days_rolling_weighted));
            ...........
        }

,然后在测试配置中添加为databaseService创建的实例。

public class DatabaseServiceImplTestConfig {

   @Bean
   public DatabaseService databaseService() {
    return new DatabaseServiceImpl();
   }
}