Development/Java

[Spring] Springboot JUnit 테스트 시 설정

반응형

1. pom.xml 설정


1) Junit 4.12 버전 이상

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>


2) Spring-boot starter-test

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>


테스트 클래스 구성


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

/**
 * Created by stacks5978 on 2017-05-19.
 */

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class PoolTest {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Before
    public void setUp() throws Exception {

    }

    @After
    public void tearDown() throws Exception {

    }

    @Test
    public void test() throws Exception {
        logger.info("Tested");
    }
}


그냥 어노테이션만 명시해도 테스트가 가능하다(Spring boot인 경우)


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

/**
 * Created by stacks5978 on 2017-05-19.
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class PoolTest {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Before
    public void setUp() throws Exception {

    }

    @After
    public void tearDown() throws Exception {

    }

    @Test
    public void test() throws Exception {
        logger.info("Tested");
    }
}


반응형