테스트 NG를 사용한 스프링 종속성 주입
스프링 지원 J는 다음과 같은 점에서 매우 적합합니다.와 함께RunWith
그리고.ContextConfiguration
주석, 사물은 매우 직관적으로 보입니다.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:dao-context.xml")
이 테스트는 Eclipse 및 Maven에서 모두 잘못 실행될 수 있습니다.TestNG에도 비슷한 것이 있는지 궁금합니다.저는 이 "차세대" 프레임워크로 이동할 것을 고려하고 있지만 Spring과 테스트할 만한 것을 찾지 못했습니다.
TestNG에서도 작동합니다.테스트 클래스는 다음 클래스 중 하나를 확장해야 합니다.
org.springframework.test.context.testng.AbstractTestNGSpringContextTests
org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests
여기 제게 도움이 된 예가 있습니다.
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
@Test
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestValidation extends AbstractTestNGSpringContextTests {
public void testNullParamValidation() {
// Testing code goes here!
}
}
Spring과 TestNG는 잘 작동하지만 주의해야 할 점이 있습니다.하위 분류를 제외하고AbstractTestNGSpringContextTests
표준 테스트 NG 설정/분해 주석과 상호 작용하는 방식을 알아야 합니다.
테스트 NG에는 네 가지 수준의 설정이 있습니다.
- Before Suite
- 테스트 전
- 수업 전
- 비포 메소드
이는 예상대로 정확히 발생합니다(자체 문서화 API의 좋은 예).이들은 모두 다음과 같은 선택적 값을 가집니다.dependsOnMethods
문자열 또는 문자열[]을 사용할 수 있습니다. 이는 동일한 수준의 메서드 이름 또는 이름입니다.
그AbstractTestNGSpringContextTests
수업이 있습니다.BeforeClass
라고 하는 주석이 달린 방법springTestContextPrepareTestInstance
자동 유선 클래스를 사용하는 경우 종속되도록 설정 방법을 설정해야 합니다.메서드의 경우 자동 배선은 클래스 이전 메서드에서 테스트 클래스가 설정될 때 발생하므로 걱정할 필요가 없습니다.
이것은 당신이 어떻게 자동 배선 클래스를 주석이 달린 방법에서 사용할 수 있는지에 대한 질문을 남깁니다.BeforeSuite
수동으로 호출하여 이 작업을 수행할 수 있습니다.springTestContextPrepareTestInstance
기본적으로 이 작업을 수행하도록 설정되지는 않았지만 여러 번 성공적으로 수행했습니다.
다음은 Arup의 예제를 수정한 버전입니다.
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
@Test
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestValidation extends AbstractTestNGSpringContextTests {
@Autowired
private IAutowiredService autowiredService;
@BeforeClass(dependsOnMethods={"springTestContextPrepareTestInstance"})
public void setupParamValidation(){
// Test class setup code with autowired classes goes here
}
@Test
public void testNullParamValidation() {
// Testing code goes here!
}
}
언급URL : https://stackoverflow.com/questions/2608528/spring-dependency-injection-with-testng
'programing' 카테고리의 다른 글
f:ajax on 이벤트 함수에 추가 매개 변수 전달 (0) | 2023.08.15 |
---|---|
Github 저장소에서 파일 가져오기 시도: "관련 없는 기록 병합 거부" (0) | 2023.08.15 |
jQuery - 클래스의 요소에서 속성 값 목록을 가져옵니다. (0) | 2023.08.15 |
가능한 AOP 사용 사례는 무엇입니까? (0) | 2023.08.15 |
Apache POI를 사용하여 .xls와 .xlsx 파일을 모두 읽을 수 있는 방법이 있습니까? (0) | 2023.08.15 |