用Mockito注入具有真实参数的bean

时间:2018-10-11 08:47:38

标签: java spring unit-testing mockito spring-test

我正在尝试使用Mockito编写一个单元测试用例,我想注入一个带有真实参数而不是模拟参数的bean。

该bean具有一些从.properties文件读取的字符串值。

@Component
public class SomeParameters {

    @Value("${use.queue}")
    private String useQueue;

 }

@RunWith(MockitoJUnitRunner.class)
public class ServiceTest {

    @Mock
    private A a;

    @Autowired
    private SomeParameters someParameters;

    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);

    }

    @Test
    public void testMethod() {
        if(someParameters.getUseQueue==true){
            //do something
        }else{
            /bla bla
        }
    }

我的主要目标是在真实场景下运行测试用例。我不想使用模拟值。

我能够以这种方式注入具有真实参数的bean。但这是单元测试用例,而不是集成测试。所以我不应该给applicationContext。您能指导我如何处理这种情况吗?

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContextTest.xml"})
public class ServiceTest {

2 个答案:

答案 0 :(得分:1)

如果要使用 spring-context ,则应为测试创建一个配置(通过xml或java config),并仅声明所需的bean。 For Example

要设置属性,只需声明import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { final fullname = TextFormField( style: TextStyle(color: Colors.white, fontSize: 17.0), decoration: InputDecoration( icon: Icon(Icons.person_outline, size: 30.0, color: Colors.white), labelText: "FULL NAME", labelStyle: TextStyle(color: Colors.white, height: 0.5, fontSize: 15.0), contentPadding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0,15.0), border: InputBorder.none, ), ); final username = TextFormField( keyboardType: TextInputType.emailAddress, style: TextStyle(color: Colors.white, fontSize: 17.0), decoration: InputDecoration( icon: Icon(Icons.alternate_email, size: 30.0, color: Colors.white), labelText: "EMAIL ADDRESS", labelStyle: TextStyle(color: Colors.white, height: 0.5, fontSize: 15.0), contentPadding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0,15.0), border: InputBorder.none, ), ); final mobilenumber = TextFormField( keyboardType: TextInputType.number, style: TextStyle(color: Colors.white, fontSize: 17.0), decoration: InputDecoration( icon: Icon(Icons.phone_android, size: 30.0, color: Colors.white), labelText: "MOBILE NUMBER", labelStyle: TextStyle(color: Colors.white, height: 0.5, fontSize: 15.0), contentPadding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0,15.0), border: InputBorder.none, ), ); final registerButton = Padding( padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0), child: FlatButton( shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)), padding: EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 20.0), onPressed: (){ }, color: Colors.pink, child: Text("Register", style: TextStyle(color: Colors.white, fontSize: 18.0)), ), ); final password = TextFormField( obscureText: true, style: TextStyle(color: Colors.white, fontSize: 17.0), decoration: InputDecoration( icon: Icon(Icons.lock_outline, size: 30.0, color: Colors.white), labelText: "PASSWORD", labelStyle: TextStyle(color: Colors.white, height: 0.5, fontSize: 15.0), contentPadding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0,15.0), border: InputBorder.none, ), ); final repassword = TextFormField( obscureText: true, style: TextStyle(color: Colors.white, fontSize: 17.0), decoration: InputDecoration( icon: Icon(Icons.lock_outline, size: 30.0, color: Colors.white), labelText: "RE-TYPE PASSWORD", labelStyle: TextStyle(color: Colors.white, height: 0.5, fontSize: 15.0), contentPadding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0,15.0), border: InputBorder.none, ), ); List<DropdownMenuItem<String>> genderNames = []; String selectedItem; void loadGenders(){ genderNames = []; genderNames.add(new DropdownMenuItem(child: new Text("Male"), value: "male",)); genderNames.add(new DropdownMenuItem(child: new Text("Female"), value: "female")); } loadGenders(); final genderDropDown = Container( child : Row( children: <Widget>[ Icon(Icons.wc, size: 30.0, color: Colors.white), SizedBox(width: 15.0), DropdownButtonHideUnderline( child: DropdownButton( value: selectedItem, items: genderNames, hint: Text("SELECT GENDER"), iconSize: 40.0, onChanged: (value) { selectedItem = value; /*setState(() { });*/ }, ), ) ], ) ); final inputboarder = new Container( height: 1.0, margin: EdgeInsets.fromLTRB(0.0, 7.0, 0.0, 0.0), decoration: BoxDecoration( image: DecorationImage( image: new AssetImage("assets/tmp/birds.jpg"), ) ), ); return new MaterialApp( home: new Scaffold( appBar: new AppBar( title: new Text('Flutter Demo'), elevation: 0.0, backgroundColor: Colors.transparent, //Something to pin the appbar ), body : Container( decoration: new BoxDecoration( image: new DecorationImage( image: new AssetImage("assets/tmp/birds.jpg"), fit: BoxFit.fill, ), ), child: new Stack( children: <Widget>[ Container( padding: EdgeInsets.only(left: 40.0, right: 40.0), child: ListView( shrinkWrap: true, children: <Widget>[ SizedBox(height: 58.0), fullname, inputboarder, SizedBox(height: 28.0), username, inputboarder, SizedBox(height: 28.0), password, inputboarder, SizedBox(height: 28.0), repassword, inputboarder, SizedBox(height: 28.0), mobilenumber, inputboarder, SizedBox(height: 28.0), genderDropDown, inputboarder, SizedBox(height: 28.0), registerButton ], ) ), ], ), ), ) ); } } ,否则您需要从测试资源中读取值。

PS。还要检查@MockBean and @SpyBean,尤其是@TestPropertiesSource("use.queue=someValue")

答案 1 :(得分:0)

如果要使用实际属性,请使用Properties对象加载属性文件,并通过从Properties对象获取值来模拟值。