Springwriting spring junit testcases
2015 · 03 · 24
1 min read
Paper
Contents
Add the following to pom.xml.
1
2
3
4
5
6
| <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.5.RELEASE</version>
<scope>test</scope>
</dependency>
|
Test java code MemberServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( {
"classpath:servlet-context.xml",
"classpath:config/context-datasource.xml"
}
)
public class MemberServiceTest {
@Autowired
MemberService memberService;
@Test
public void testSr2002() throws Exception {
RequestData req = new RequestData(null, new DbMap());
ResponseData res = new ResponseData(new DbMap());
memberService.sr2002(req, res);
}
}
|
Since the DB uses jndi-lookup, I put test/resources/config/context-datasource.xml in place to override the existing bean id like below.
test/resources/config/context-datasource.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://server:3306/dbname"
p:username="sa"
p:password="" />
</beans>
|