你好我是一名住在韩国的大学生。 我一直在研究这个过程,我在正在进行的项目的单元测试(Junit)中遇到错误
我尝试了3天谷歌,但它没有成功。所以,我需要帮助。
这适用于UserDaoTest。但是,没有测试BoardDaoTest的一种方法。我在这里附上我的源代码。
JdbcTemplate.java
public class JdbcTemplate extends JdbcDaoSupport {
private static final Logger logger = LoggerFactory.getLogger(JdbcTemplate.class);
public static Connection conn;
public PreparedStatement pstmt;
public PreparedStatement pstmt2;
public ResultSet rs;
@PostConstruct
public void initialize() {
DataSource ds = getDataSource();
logger.info("get Data Source success!");
conn = DataSourceUtils.getConnection(ds);
logger.info("Conn = " + conn);
logger.info("database initialized success!");
} { ... }
的applicationContext.xml
<context:annotation-config />
<context:property-placeholder location="classpath*:application-properties.xml" />
<bean id="applicationContextProvider" class="viser.service.ApplicationContextProvider" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="${database.driverClassName}"
p:url="${database.url}"
p:username="${database.username}"
p:password="${database.password}"
/>
<bean id="jdbcTemplate" class="viser.service.support.jdbc.JdbcTemplate"
p:dataSource-ref="dataSource"
/>
<bean id="userDAO" class="viser.dao.user.UserDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="boardDAO" class="viser.dao.board.BoardDAO">
<property name="dataSource" ref="dataSource" />
</bean>
UserDAOTest.java(成功测试)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/applicationContext.xml")
public class UserDAOTest {
@Autowired
private UserDAO userDAO;
private User user;
@Before
public void setUp() {
user = UserTest.TEST_USER;
}
@After
public void returns() throws SQLException {
userDAO.removeUser(user.getUserId());
}
@Test
public void crud() throws Exception {
{ ... }
}
@Test
public void getWhenNotExsitUser() throws Exception {
{ ... }
}
BoardDAOTest.java(crud() - &gt; success,getList() - &gt;失败)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/applicationContext.xml")
public class BoardDAOTest {
private static final Logger logger = LoggerFactory.getLogger(BoardDAOTest.class);
public static Board TEST_BOARD = new Board("TEST_BOARD",
`ProjectDAOTest.TEST_PROJECT.getProjectName());`
@Autowired
private BoardDAO boardDAO;
private ProjectDAO projectDAO;
@Before
public void setup() throws SQLException {
boardDAO = new BoardDAO();
projectDAO = new ProjectDAO();
projectDAO.addProject(ProjectDAOTest.TEST_PROJECT);
}
@After
public void returns() throws SQLException {
projectDAO.removeProject(ProjectDAOTest.TEST_PROJECT.getProjectName());
}
@Test
public void crud() throws SQLException {
{...}
}
@Test
public void getList() throws SQLException {
logger.debug("list : {}", boardDAO.getBoardList(ProjectDAOTest.TEST_PROJECT.getProjectName())); assertNotNull(boardDAO.getBoardList(ProjectDAOTest.TEST_PROJECT.getProjectName()));
}
答案 0 :(得分:0)
您的代码存在一些问题:
viser.service.support.jdbc.JdbcTemplate
。这是不好的做法。使用现有的Spring JdbcTemplate。@Before
方法的Test类中,您手动初始化DAO:new BoardDAO() / new ProjectDAO()
。这违背了进行Spring Integration测试的目的。使用现有的@Autowired
方法从测试上下文中获取它们。答案 1 :(得分:0)
您正在使用自己的初始化DAO替换Autowired DAO,这些DAO缺少数据源。您正在设置方法中这样做。那是你的问题。
从该设置方法中删除自定义的;或删除Autowired注释并确保您的设置方法正确构建它们。