简体   繁体   English

如何在不进行硬编码的情况下为springboot中的方法编写Junit测试用例

[英]How to write a Junit test case for a method in springboot without hardcoding

New to junit test case and want to know the test case for my code here without hardcoding values. junit测试用例的junit并且想在此处了解我的代码的测试用例,而无需对值进行硬编码。

My code is 我的代码是

public JSONObject getListOfAllForms() {

    List<Forms> forms = FormName.getInstance().getformList();
    int totalNumberOfforms = 0;
    List<String> formIds = new ArrayList<String>();
    try {
        for (int i = 0; i < forms.size(); i++) {
            form formOb = forms.get(i);
            formIds.add(formOb.getformId());
            totalNumberOfforms = forms.size();

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    JSONObject formsListObject = new JSONObject();
    formsListObject.put("formIds", formIds);
    formsListObject.put("totalNumberOfforms", totalNumberOfforms);
    return formsListObject;
}

My controller code is: 我的控制器代码是:

@RequestMapping(value = "/new/getforms/{formid}", method = RequestMethod.GET)
public JSONObject getFormByFormId(@PathVariable("formid") String formid) {
    return newFormName.getformByformId(formid);
}

If you want to test getListOfAllForms Your problem is basically this line... 如果您想测试getListOfAllForms基本上您的问题是这一行...

List<Forms> forms = FormName.getInstance().getformList();

This line couples your FormName hard into your method. 这行代码将您的FormName硬耦合到您的方法中。 This is bad. 这不好。

A better way would be to provide the FormName instance when instantiating your class, for example, let's assume the name of your class was MyClass .. 更好的方法是在实例化类时提供FormName实例,例如,假设类的名称为MyClass

private FormName formName;

public MyClass(FormName formName) {
     this.formName = formName;
}

This can be used via Spring or manually. 可以通过Spring或手动使用。 Via Spring your FormName instance just needs to be a bean, then you can add @Autowired to the constructor if MyClass itself is a bean. 通过Spring,您的FormName实例只需要是一个bean,然后如果MyClass本身是bean,则可以将@Autowired添加到构造函数中。

What is the advantage now? 现在有什么优势? Easy, for a test case, you can now do the simple thing... 简单,对于一个测试用例,您现在可以做简单的事情...

public void someGreatTestName() {
    FormName formName = ...; // either create a new, fake one or mock one (see below)
    JSONObject object = new MyClass(formName).getListOfAllForms();

    // now test that the object is correct
}

Doing this means you can inject a fake or mock FormName there, which will return the data you want for the test and so remove the need to have an actual "live" FormName there. 这样做意味着您可以在此处注入假的或模拟的FormName ,这将返回您想要进行测试的数据,因此无需在此处具有实际的“活动” FormName In other words, you can fake/mock the dependency to the FormName . 换句话说,您可以伪造/模拟对FormName的依赖关系。 This way, your test uses test data and you don't have to hardcode the live values there. 这样,您的测试将使用测试数据,而无需在此处对实时值进行硬编码。

Mocking can be done easily via Mockito, for example, I suggest giving it a try, but in this case, simply creating a new "fake" FormName may suffice. 可以通过Mockito轻松完成模拟,例如,我建议尝试一下,但是在这种情况下,只需创建一个新的“假” FormName就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM