Spring Junit 和基于注解的自动装配
Spring Junit and annotation based autowiring
我在一个简单的 spring 示例中添加了一个 junit 测试,但它无法自动装配我编写的 json 服务。
让自动装配在 spring JUnit 测试中工作需要什么?
要尝试失败的项目,请执行...
git clone https://bitbucket.org/oakstair/spring-boot-cucumber-example
cd spring-boot-cucumber-example
./gradlew test
提前致谢!
申请
@SpringBootApplication
@ComponentScan("demo")
public class DemoApplication extends SpringBootServletInitializer {
服务接口
@Service
public interface JsonUtils {
<T> T fromJson(String json, Class<T> clazz);
String toJson(Object object);
}
服务实施
@Component
public class JsonUtilsJacksonImpl implements JsonUtils {
测试
@ContextConfiguration()
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan("demo")
public class JsonUtilsTest {
@Autowired
private JsonUtils jsn;
在您的 JsonUtilsTest 中,您不能将 @ComponentScan 放在此处的 class 级别,因为它不是 @Configuration class。使用 @ContextConfiguration 注释,就像您在此处使用的那样,它首先寻找静态内部 @Configuration class 因此添加其中一个带有 @ComponentScan 的注释,它应该可以工作:
@ContextConfiguration()
@RunWith(SpringJUnit4ClassRunner.class)
public class JsonUtilsTest {
@Autowired
private JsonUtils jsn;
@Test
// Note: This test is not tested since I haven't got autowiring to work.
public void fromJson() throws Exception {
Integer i = jsn.fromJson("12", Integer.class);
assertEquals(12, (int) i);
}
@Test
// Note: This test is not tested since I haven't got autowiring to work.
public void toJson() throws Exception {
assertEquals("12", jsn.toJson(new Integer(12)));
}
@Configuration
@ComponentScan("demo")
public static class TestConfiguration {
}
}
编辑:或者您可以通过使用带有 SpringRunner 的 @SpringBootTest 注释来让 Spring 引导为您完成工作:
@RunWith(SpringRunner.class)
@SpringBootTest
public class JsonUtilsTest {
将此添加到测试中 class 解决了我的问题!
@ContextConfiguration(classes = {DemoApplication.class})
添加@SpringBootTest
在你的测试中class
并将您的 SpringBootApplication class 和 Json utils class 提供给 @SpringBootTest
的 classes 字段
它应该是这样的
@ContextConfiguration()
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={<package>.DemoApplication.class, <package>.JsonUtil.class } )
@ComponentScan("demo")
public class JsonUtilsTest {
我在一个简单的 spring 示例中添加了一个 junit 测试,但它无法自动装配我编写的 json 服务。
让自动装配在 spring JUnit 测试中工作需要什么?
要尝试失败的项目,请执行...
git clone https://bitbucket.org/oakstair/spring-boot-cucumber-example
cd spring-boot-cucumber-example
./gradlew test
提前致谢!
申请
@SpringBootApplication
@ComponentScan("demo")
public class DemoApplication extends SpringBootServletInitializer {
服务接口
@Service
public interface JsonUtils {
<T> T fromJson(String json, Class<T> clazz);
String toJson(Object object);
}
服务实施
@Component
public class JsonUtilsJacksonImpl implements JsonUtils {
测试
@ContextConfiguration()
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan("demo")
public class JsonUtilsTest {
@Autowired
private JsonUtils jsn;
在您的 JsonUtilsTest 中,您不能将 @ComponentScan 放在此处的 class 级别,因为它不是 @Configuration class。使用 @ContextConfiguration 注释,就像您在此处使用的那样,它首先寻找静态内部 @Configuration class 因此添加其中一个带有 @ComponentScan 的注释,它应该可以工作:
@ContextConfiguration()
@RunWith(SpringJUnit4ClassRunner.class)
public class JsonUtilsTest {
@Autowired
private JsonUtils jsn;
@Test
// Note: This test is not tested since I haven't got autowiring to work.
public void fromJson() throws Exception {
Integer i = jsn.fromJson("12", Integer.class);
assertEquals(12, (int) i);
}
@Test
// Note: This test is not tested since I haven't got autowiring to work.
public void toJson() throws Exception {
assertEquals("12", jsn.toJson(new Integer(12)));
}
@Configuration
@ComponentScan("demo")
public static class TestConfiguration {
}
}
编辑:或者您可以通过使用带有 SpringRunner 的 @SpringBootTest 注释来让 Spring 引导为您完成工作:
@RunWith(SpringRunner.class)
@SpringBootTest
public class JsonUtilsTest {
将此添加到测试中 class 解决了我的问题!
@ContextConfiguration(classes = {DemoApplication.class})
添加@SpringBootTest
在你的测试中class 并将您的 SpringBootApplication class 和 Json utils class 提供给 @SpringBootTest
的 classes 字段它应该是这样的
@ContextConfiguration()
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={<package>.DemoApplication.class, <package>.JsonUtil.class } )
@ComponentScan("demo")
public class JsonUtilsTest {