我有一个配置类和测试类,如下:如何测试一个具有原型范围的spring bean?
@Configuration
public class SimpleConfiguration {
@Bean
@Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public String hello() {
return "hello";
}
}
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = SimpleConfiguration.class)
public class SimpleTest {
@Autowired
private String hello;
@Autowired
private String another;
@Test
public void should_not_same() {
Assert.assertNotSame(hello, another);
}
}
据prototype scope
定义,hello
和another
对象应该是不一样的,但是这个测试失败。为什么?谢谢!
对于你的实验,你选择了唯一不适合Spring Bean的类型:一个字符串。另外,你为什么试图测试Spring框架代码? – kryger