Junit 의 환경 설정
	<!-- test -->
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-test</artifactId>
		    <version>5.2.18.RELEASE</version>  <!--하단에 TEST Junit이 있는데 그냥 위에다 붙였다...자바 버전과 일치해야하기때문에 체크해야한다.  -->
		    <scope>test</scope>
		</dependency><!-- Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>pom.xml수정


@Repository
public class CustomerStoreLogic {
	@Autowired
	private SqlSession sqlSession;    //인터페이스 결합도를 약하게 하게 위해  //templi을 쓰면 결합도가 강해서 나중에 session으로 변경된다.
	
	public int registerCustomer(Customer customer) {
		int result = sqlSession.insert("",customer);
		return result;
	}
}customer패키지// domain 패키지//store패키지와 클래스들 만들고 클래스만들면 get,set 기본생성자 매개변수 생성자 만들건 기본..

테스트의 클래스는 test파일에 한다.
이 폴더가 왜있나 했더니 이제서야 써본다..
coustomer 패키지 우클릭..클래스 만드는데.....좀 다름...
우클릭..new...other .. junit으로 검색해서 test Case 파일로 선택한다.


여기서 테스트할 클래스를 선택한것이다. 아까만들었던 logic을 클래스를 선택하고 next를 해서 메소드 선택한다.


인제 여기서 작업을 한다.
package com.kh.junspring.customer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.kh.junspring.customer.domain.Customer;
import com.kh.junspring.customer.store.CustomerStoreLogic;
@RunWith(SpringJUnit4ClassRunner.class)   //스프링에서 JUnit을 사용할수 있도록 하는 클래스  //부트에서는적을 필요는 없다.
@ContextConfiguration(locations= {"file:src/main/webapp/WEB-INF/spring/root-context.xml"})  //중괄호는 여러개를 쓸수도 있다.
public class CustomerStoreLogicTest {
	
	@Autowired  //꼭 해줘야 한다.
	private CustomerStoreLogic store;
	@Test
	public void testRegisterCustomer() {
		//fail("Not yet implemented");
		Customer customer = new Customer();
		customer.setId("khuser01");
		customer.setName("일용자");
		customer.setAge(21);
		
		int result = store.registerCustomer(customer);
//		if(result==1) {
//			System.out.println("성공");
//		}else {
//			System.out.println("실패");
//		} 이것을 대신하여 작업할수 있게 해준다
		assertEquals(1,result);
		
	}
}했는데 에러난다.....왜냐면 연결한 CustomerStoreLogic와 연결한 것을 찾을 수가 없었던것이다....이걸 직접 연결할수 있도록 @Repository 를 빼고 root-context.xml에 한줄 더 추가한다.
<bean id="customerStore" class="com.kh.junspring.customer.store.CustomerStoreLogic"></bean>또는
	"file:src/main/webapp/WEB-INF/spring/appServler/servlet-context.xml"})넣으면 어노테이션으로 할 수도 있다.
그런데 어노테이션을 하려고 2번째방법을 썼더니...이해할수 없는 에러 발생.....
java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler ["/resources/"]] does not run in a ...
난 이게 먼지 잘 모르겠다.......servlet-context.xml 파일을 못읽는다는것라는데 잘모르겠음.....
역시 구글링..!!!!
오류내용 그대로 했더니...

@WebAppConfiguration 붙이면 끝......
된다..^^
Member 로 연습



이건 에러가 난게 아닌다.....메소드가 5개 있는데 그중 한개만 작성해서 진행했고 그게 된거니깐^^ 결과물이 나온다.....앗싸!!!!!!
추가로 한개 더!! select값이 나오는데...에러는 아닌데 실패로 잡힌다.......한가지 빼머었다..
assertEquals("abcd4",mOne.getMemberId()); 결과값에 관련된 구문을 넣을때 예시다....
결과값중에..mOne.getMemberId() 의 결과값이 abcd4어야 정상이란 주문임..^^


'junit TEST' 카테고리의 다른 글
| junit private method ...and void (0) | 2023.02.22 | 
|---|---|
| Junit Test builder() (0) | 2023.02.22 | 
| Test 연습하기 (0) | 2023.02.01 | 
| TEST 로 --일단은 환경설정과 싸움/..완성시키기 (0) | 2022.11.16 |