programing

테스트 NG를 사용한 스프링 종속성 주입

lastmoon 2023. 8. 15. 11:25
반응형

테스트 NG를 사용한 스프링 종속성 주입

스프링 지원 J는 다음과 같은 점에서 매우 적합합니다.와 함께RunWith그리고.ContextConfiguration주석, 사물은 매우 직관적으로 보입니다.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:dao-context.xml")

이 테스트는 Eclipse 및 Maven에서 모두 잘못 실행될 수 있습니다.TestNG에도 비슷한 것이 있는지 궁금합니다.저는 이 "차세대" 프레임워크로 이동할 것을 고려하고 있지만 Spring과 테스트할 만한 것을 찾지 못했습니다.

TestNG에서도 작동합니다.테스트 클래스는 다음 클래스 중 하나를 확장해야 합니다.

여기 제게 도움이 된 예가 있습니다.

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

반응형