본문 바로가기
[개발] Test

JUnit Test code setup

by Devsong26 2023. 11. 13.

버전 정보
Java 1.8

Spring Boot 1.4.6.RELEASE

 

Class 정보

@Service("compositionService")
public class CompositionService {

	private final DependencyBean dependencyBean;
    
    public CompositionService(DependencyBean dependencyBean){
		this.dependencyBean = dependencyBean;
	}
    
    // business methods...
    
}

 

 

Mock을 이용한 JUnit setup

@RunWith(MockitoJUnitRunner.class)
public class TestClazz {

	@Mock
    DependencyBean dependencyBean
    
    @InjectionMocks
    CompositionService compositionService

}

 

 

@SpringBootTest를 이용한 JUnit setup

@Transactional
@ActiveProfiles("#{profile}")
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestClass {

	@Autowired
    CompositionService compositionService
    
    // test codes...

}

 

 

@ContextConfiguration을 이용한 JUnit setup

@Transactional
@ActiveProfiles("#{profile}")
@RunWith(SpringRunner.class)
@ContextConfiguration(
	classes = {
    	DependencyBean.class,
        CompositionService.class
    }
)
public class TestClass {

	@Autowired
    CompositionService compositionService;
    
    // Test codes...

}

'[개발] Test' 카테고리의 다른 글

화이트박스 테스트  (0) 2023.12.31
험블 객체 패턴  (0) 2023.12.15
과연 TDD를 제대로 알고 있는 걸까?  (0) 2022.03.27
Service, Controller 테스트  (0) 2018.08.05
JUnit  (0) 2018.01.27