Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Analysis
- LIST
- javascript
- git
- Spring
- Web
- SSH
- Deep
- 자바
- data
- Pattern
- centos
- 인공지능
- db
- interface
- java
- Security
- Server
- ai
- Linux
- 함수
- DeepLearning
- learning
- Python
- error
- Github
- mysql
- framework
- mariaDB
- Numpy
Archives
- Today
- Total
PostIT
[Spring/TDD/JUnit]How to Spring MVC Unit Test 스프링 MVC 단위 테스트 - 퍼옴 본문
Spring/Etc
[Spring/TDD/JUnit]How to Spring MVC Unit Test 스프링 MVC 단위 테스트 - 퍼옴
shun10114 2017. 3. 3. 13:24https://www.mkyong.com/unittest/junit-spring-integration-example/
* 스프링 MVC 단위 테스트!
In this tutorial, we will show you how to test the Spring DI components with JUnit frameworks.
Technologies used :
- JUnit 4.12
- Hamcrest 1.3
- Spring 4.3.0.RELEASE
- Maven
1. Project Dependencies
To integrate Spring with JUnit, you need spring-test.jar
pom.xml
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.0.RELEASE</version>
<scope>test</scope>
</dependency>
2. Spring Components
A simple Spring components, later for testing.
2.1 An interface.
DataModelService.java
package com.mkyong.examples.spring;
public interface DataModelService {
boolean isValid(String input);
}
2.2 Implementation of above interface.
MachineLearningService.java
package com.mkyong.examples.spring;
import org.springframework.stereotype.Service;
@Service("ml")
public class MachineLearningService implements DataModelService {
@Override
public boolean isValid(String input) {
return true;
}
}
2.2 A Spring configuration file, component scanning.
AppConfig.java
package com.mkyong.examples.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = {"com.mkyong.examples.spring"})
public class AppConfig {
}
3. JUnit + Spring Integration Examples
Annotate the JUnit test class with @RunWith(SpringJUnit4ClassRunner.class)
and loads the Spring configuration file manually. Refer below :
MachineLearningTest.java
package com.mkyong.spring;
import com.mkyong.examples.spring.AppConfig;
import com.mkyong.examples.spring.DataModelService;
import com.mkyong.examples.spring.MachineLearningService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class})
public class MachineLearningTest {
//DI
@Autowired
@Qualifier("ml")
DataModelService ml;
@Test
public void test_ml_always_return_true() {
//assert correct type/impl
assertThat(ml, instanceOf(MachineLearningService.class));
//assert true
assertThat(ml.isValid(""), is(true));
}
}
Done.
4. FAQs
4.1 For XML, try this :
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(locations = {
"classpath:pathTo/appConfig.xml",
"classpath:pathTo/appConfig2.xml"})
public class MachineLearningTest {
//...
}
4.2 For multiple configuration files :
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(classes = {AppConfig.class, AppConfig2.class})
public class MachineLearningTest {
//...
}
References
'Spring > Etc' 카테고리의 다른 글
[Spring/Lombok] Equals 와 Hashcode 사용하기 - 퍼옴 (0) | 2017.03.29 |
---|---|
[JSP/JSTL] JSTL, foreach에서의 varStatus 속성 이용 (0) | 2017.02.03 |
[Spring/JSTL] JSTL에서 값 비교하기 - 퍼옴 (0) | 2016.12.21 |
[Http] Http 에러코드 메세지 정리 - 퍼옴 (0) | 2016.11.30 |
[Spring/Java]Spring request scope error : No thread-bound request found - 퍼옴 (아이피 확인시 문제발생) (0) | 2016.11.22 |
Comments