npe for jdbctemplate,同时使用h2在SpringMVC中进行测试

xqnpmsa8  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(220)

我试图在springmvc上测试我的项目,postgresql使用内存中的dbh2。我收到jdbctemplate的npe。我认为配置文件有一些问题,但我不知道哪里出了问题。请帮忙)
我的分组测试:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = TestConfigClass.class)
class GroupDaoImplTest {

    @Autowired
    private GroupDaoImpl groupDao;

    @Autowired
    private GroupServiceImpl groupService;

    @BeforeEach
    void setUp() {

        TestDataPreparation testDataPreparation = new TestDataPreparation();
        testDataPreparation.preparedDataForTests();

    }

    @Test
    void addGroup() {
    }
}

我的testconfigclass:

@Configuration
@PropertySource(value = { "classpath:application.properties" })
@ComponentScan(basePackages = "com.foxminded.learning.impl")
public class TestConfigClass{

private static final Logger logger = LogManager.getLogger(TestConfigClass.class);

@Autowired
private Environment env;

@Bean
public GroupServiceImpl groupService() {return new GroupServiceImpl();}

@Bean
public LectureServiceImpl lectureService() {return new LectureServiceImpl();}

@Bean
public StudentServiceImpl studentService() {return new StudentServiceImpl();}

@Bean
public SubjectServiceImpl subjectService() {return new SubjectServiceImpl();}

@Bean
public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(env.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(env.getRequiredProperty("jdbc.password"));

        logger.info("All configuration successfully utilized");

        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.setResultsMapCaseInsensitive(true);

        return jdbcTemplate;
    }

}

我的测试数据准备:

public class TestDataPreparation {

    private static final Logger logger = LogManager.getLogger(TestDataPreparation.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void preparedDataForTests() {

        logger.traceEntry("Initialize tables and fill them");

        jdbcTemplate.update("CREATE TABLE subjects (subjectId SERIAL, title VARCHAR(255), description 
    TEXT, PRIMARY KEY (subjectId))");
        jdbcTemplate.update("CREATE TABLE groups (groupId SERIAL, groupname VARCHAR(255), PRIMARY KEY 
    (groupId))");
        jdbcTemplate.update("CREATE TABLE lectures (lectureId SERIAL, firstName VARCHAR(255), lastName 
    VARCHAR(255), PRIMARY KEY (lectureId))");
        jdbcTemplate.update("CREATE TABLE students (studentId SERIAL, firstName VARCHAR(255), lastName `enter code here`VARCHAR(255), groupId INTEGER, PRIMARY KEY (studentId), FOREIGN KEY (groupId) REFERENCES `enter code here`groups(groupId))");
        jdbcTemplate.update("CREATE TABLE schedule_items (scheduleItemId SERIAL, date TIMESTAMP, `enter code here`subjectId INTEGER, groupId INTEGER, lectureId INTEGER, duration INTEGER, PRIMARY KEY `enter code here`(scheduleItemId), FOREIGN KEY (subjectId) REFERENCES subjects(subjectId), FOREIGN KEY (groupId) `enter code here`REFERENCES groups(groupId), FOREIGN KEY (lectureId) REFERENCES lectures(lectureId))");

        jdbcTemplate.update("INSERT INTO groups (groupId, groupname) VALUES (1, 'one')");
        jdbcTemplate.update("INSERT INTO groups (groupId, groupname) VALUES (2, 'two')");
        jdbcTemplate.update("INSERT INTO groups (groupId, groupname) VALUES (3, 'three')");

    }
}

我收到:

java.lang.NullPointerException  at 
com.foxminded.learning.service.TestDataPreparation.preparedDataForTests(TestDataPreparation.java:25)
at com.foxminded.learning.impl.GroupDaoImplTest.setUp(GroupDaoImplTest.java:28)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题