如何设置集成测试? H2 Spring集成测试不断抛出nullValueException

时间:2017-03-31 12:19:35

标签: spring eclipse junit mockito h2

我想在Spring引导应用程序中使用H2,Mockito和Junit在STS中测试存储库类。我无法真正设置环境,它会一次又一次地抛出异常。

java.lang.IllegalArgumentException:需要WebApplicationContext 显示java.lang.NullPointerException

启用了空例外     jdbc.getDataSource()。getConnection(),

我应该如何连接H2连接?

@RunWith(MockitoJUnitRunner.class)
@SpringApplicationConfiguration(classes=XZYApplication.class)
@WebIntegrationTest
public class XZYApplicationTests {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @InjectMocks // what we want to test
    GlobalController globalController;

    @Mock // class we want to test, inside autowired dependencies
    JdbcTemplate jdbc;

    private static final String sqlScript_Create = "scripts.create.tables.sql";
    private static final String sqlScript_Drop   = "scripts.drop.tables.sql";

    @Before
    public void setup(){
        this.mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Before
    public void before() throws SQLException {
        ScriptUtils.executeSqlScript (
            jdbc.getDataSource().getConnection(), 
            new ClassPathResource(sqlScript_Create) 
            );
        }

    @After
    public void after() throws SQLException {
        ScriptUtils.executeSqlScript (
            jdbc.getDataSource().getConnection(), 
            new ClassPathResource(sqlScript_Drop)
        );
    }

    @Test
    public void testet(){

        AppProperties app = new AppProperties();
        app.setSchemaName("testschemaName");
        app.setVersion("testversion");
        app.setStandardLocation("standardLocation");

        Mockito.when(globalController.getAppProperties()).thenReturn(app);

        AppProperties response = globalController.getAppProperties();

        assertEquals("testschemaName", response.getSchemaName());
        assertEquals("testversion", response.getVersion());
        assertEquals("standardLocation",    response.getStandardLocation());

        // Mockito.when(jdbc.execute("sql")).thenReturn(null);

        assertEquals(1,1);
    }
}

0 个答案:

没有答案