Junit,Spring-Test란?(의존성예제)


Junit

JunitTest클래스작성

Spring-Test


Junit특징

- TDD 테스트가 가장 중요하다.

- assert메소드제공한다.(테스트결과제공 예상값,실제값제공함.)

- Junit4부터는 테스트 지원하는 어노테이션제공한다.(@Test,@Before,@After)

- 각@Test 메서드가 호출할때 마다 새로운 인스턴스를 생성하여 독립적인 테스트가 이루어지도록한다.


jUnit에서 지원하는 어노테이션


@Test

- 테스트하고자하는 메소드 위에 선언한다.

- 각각의 테스트 메서드가 실행될때 서로영향주지않고 각자실행시킨다.


@Ignore

- 선언된 메서드는 테스트를 실행하지않게한다.


@Before(@Test실행시 매번실행)

- @Test가 실행되기전에 반드시 실행할게 있다면 사용한다.


@After(@Test실행시 매번실행)

- @Test가 실행된 후에 실행되는 메소드이다.


@BeforeClass

@AfterClass

위와같으나 클래스가 한번생성될때만 사용한다.


테스트 결과를 확인하는 단정(Assert)메서드

-assertEquals(a,b);

-객체 일치확인한다.

-assertArratEquals(a,b);

-배열 일치확인한다.

-assertSame(a,b);

-객체의 주소값이 같는지 확인한다.

-assertTrue(a);

-조건a가 참인지 확인한다.

-assertNotNull(a);

-null이아님을 확인한다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class HelloBeanJunitTest {
 
    private ApplicationContext context;
 
     
 
    @Before
 
    public void init(){
 
        System.out.println("before check");
 
        context = new GenericXmlApplicationContext("config/beans.xml");
 
    }
 
     
 
    @Test
 
    public void bean1(){
 
        System.out.println("Junit equals check");
 
        Hello hello = (Hello) context.getBean("hello");
 
        assertEquals("HelloSpring", hello.sayHello());
 
        hello.print();
 
         
 
        Printer printer = (Printer) context.getBean("printer");
 
        assertEquals("HelloSpring", printer.toString());
 
    }
 
     
 
    @Test
 
    public void bean2() {
 
        System.out.println("Junit same check");
 
        Printer printer = (Printer) context.getBean("printer");
 
        Printer printer2 = context.getBean("printer", Printer.class);
 
        assertSame(printer, printer2);
 
    }
 
}




spring-test

Junit확장

@RunWith(SpringJunit4ClassRunner.class)

- Junit테스트 확장할때 사용한다.

- 테스트도중 ApplicationContext를 만들고관리해준다.

- @RunWith 각각의 테스트별로 객체가 생성되더라도 싱글톤의 ApplicationContext를 보장한다.


@ContextConfiguration

- 스프링 빈 설정파일의 위치를 지정할때 사용한다.


@Autowired

- 스프링DI에서 사용되는 특별한 어노테이션이다.

- 해당변수에 자동으로 빈을 매핑해준다.

- 스프링 빈 설정을 읽기위해 굳이 GenericXmlApplicationContext 사용할필요가없다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@RunWith(SpringJUnit4ClassRunner.class)
 
@ContextConfiguration(locations = "classpath:config/beans.xml")
 
public class HelloBeanSpringTest {
 
    @Autowired
 
    private ApplicationContext context;
 
     
 
    @Test
 
    public void bean1(){
 
        System.out.println("Junit equals check");
 
        Hello hello = (Hello) context.getBean("hello");
 
        assertEquals("HelloSpring", hello.sayHello());
 
        hello.print();
 
        assertEquals("HelloSpring", context.getBean("printer").toString());
 
         
 
        System.out.println("Junit same check");
 
        Hello hello2  = context.getBean("hello",Hello.class);
 
        hello2.print();
 
        assertSame(hello, hello2);
 
    }
 
     
 
}




'개발 소발 > 개발 Spring' 카테고리의 다른 글

Bean등록 및 메타정보구성이란?  (0) 2017.11.21
bean,properties설정이란?  (0) 2017.11.20
IoC,DI란?  (0) 2017.11.20
STS,maven이란?  (0) 2017.11.20
Spring프레임워크란?  (0) 2017.11.20

+ Recent posts