Junitテスト
ユニットを使用してスプリングユニットをテストする
ばね試験
Springフレームワークで作成されたクラス(@Controller、@Service、@Repository、
@Componentなどのクラスをテストするためのモジュール
ユニットテストと統合テストをサポートするメカニズムまたは便利な機能を提供
スプリングテスト-POM。XML設定
<dependency> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <version>${org.springframework-version}</version> <scope>test</scope>
</dependency>
<dependency> <groupId>junit</groupId>
<artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope>
</dependency>
Junit構成
テストケース-MemberTestを作成します。java
package org.tukorea.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.tukorea.di.domain.StudentVO;
import org.tukorea.di.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:/applicationContext.xml")
public class MemberTest { @Autowired
MemberService memberService;
@Test
public void testReadMember( ) throws Exception {
StudentVO member = memberService.readMember("hansol");
System.out.println(member);
}
}
その他のテストケース
package org.tukorea.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.tukorea.di.domain.StudentVO;
import org.tukorea.di.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/applicationContext.xml")
public class MemberTest {
@Autowired
MemberService memberService;
//@Test
public void testAddMember( ) throws Exception {
String strID = "JUnit1";
StudentVO vo = new StudentVO();
vo.setId(strID);
vo.setPasswd(strID);
vo.setUsername(strID);
vo.setSnum(strID);
memberService.addMember(vo);
StudentVO member = memberService.readMember("JUnit");
System.out.println(member);
}
@Test
public void testReadMember( ) throws Exception {
StudentVO member = memberService.readMember("jiyoon");
System.out.println(member);
}
}
DataSourceテストケース
package org.tukorea.test;
import java.sql.Connection;
import org.slf4j.Logger;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/applicationContext.xml")
public class DataSourceTest {
@Autowired
private DataSource ds;
private static Logger logger = LoggerFactory.getLogger(DataSourceTest.class);
@Test
public void testConntection() throws Exception {
try(Connection con = ds.getConnection()) {
logger.info("con.toString() : " + con.toString());
}catch(Exception e) {
e.printStackTrace();
}
}
}
JdbcTemplate-テストケース
package org.tukorea.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/applicationContext.xml")
public class JdbcTemplateTest {
@Autowired
private JdbcTemplate jt;
private static Logger logger =LoggerFactory.getLogger(DataSourceTest.class);
@Test
public void selectQuery() throws Exception {
try {
int count = jt.queryForObject("SELECT COUNT(*) FROM STUDENT", Integer.class);
logger.info("Count :" + count);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Reference
この問題について(Junitテスト), 我々は、より多くの情報をここで見つけました https://velog.io/@dlwldbs/Junit-테스트テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol