Junit 이란? 


Junit 테스트는 프로젝트를 구동하지 않고 코드 테스트를 지원해주는 라이브러리이다.

 

pom.xml에 의존 설정

더보기

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

 

Junit 어노테이션 정리


@Test

테스트를 수행하는 메서드를 지정한다. 각각의 테스트가 서로 영향을 주지 않고 독립적으로 실행한다.

 

@Ignore

테스트를 실행하지 않도록 해주는 어노테이션이다. 테스트를 실행할 때 포함하지 않는다.

 

@Before / @After

테스트 메소다가 실행되기 전, 후로 항상 실행되는 메소드를 지정한다. 공통적으로 실행되어야 하는 메소드에 달아주면 된다.

 

@BeforeClass / @AfterClass

각각의 메소드가 아닌 해당 클래스에서 딱 한번 수행되는 메서드이다.

 

 

@RunWith(SpringJUnit4ClassRunner.class)

ApplicationContext를 만들고 관리하는 작업을 할 수 있도록 JUnit의 기능을 확장해준다.

 

 

@ContextConfigureation(location="classpath:xml파일위치")

스프링 빈 설정 파일의 위치를 지정할 수 있다. 별도의 컨테이너를 추가하지 않고 빈을 등록해둔 xml 파일을 지정해 컨테이너에 등록해준다.

 

@Autowired

자동으로 의존성 주입을 해준다.

 

import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class PianoScoreApplicationTests {

	@Before
	void before() {
		System.out.println("before");
	}

	@Test
	void contextLoads() {
		System.out.println("Test");
	}

	@After
	void after() {
		System.out.println("after");
	}

}

+ Recent posts