在本教程中,我们将向您展示如何使用JUnit框架测试Spring DI组件。
使用的技术:
- JUnit 4.12
- Hamcrest 1.3
- 春天4.3.0.RELEASE
- 马文
1.项目依赖
要将Spring与JUnit集成,您需要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组件,稍后进行测试。
2.1接口。
DataModelService.java
package com.mkyong.examples.spring;
public interface DataModelService {
boolean isValid(String input);
}
2.2以上接口的实现。
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一个Spring配置文件,组件扫描。
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集成示例
使用@RunWith(SpringJUnit4ClassRunner.class)
注释JUnit测试类,并手动加载Spring配置文件。 请参阅以下内容:
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));
}
}
做完了
4.常见问题
4.1对于XML,请尝试以下操作:
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(locations = {
"classpath:pathTo/appConfig.xml",
"classpath:pathTo/appConfig2.xml"})
public class MachineLearningTest {
//...
}
4.2对于多个配置文件:
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(classes = {AppConfig.class, AppConfig2.class})
public class MachineLearningTest {
//...
}
参考文献
翻译自: https://mkyong.com/unittest/junit-spring-integration-example/