Search code examples
javaandroidjunitswaggerswagger-codegen

Automated Request parameter generation using swagger in DefaultTest.class file


I want to generated Model Request parameter automatically in test cases for example , in my model there are two variable with setter and getter methods as shown below.

public class LoginModel{
public String username ="abc";
public String password = "123";

public void setUserName(String username){
    this.username = username;
}
public void setPassword(String password){
    this.password = password;
}

public String getUserName(){
    return this.username;
}
public String getPassword(){
    return this.password;
}

}

and i wanted to be generated by swagger or using custom Templete in DefaultTest.class file.

 LoginModel loginModel = new LoginModel();

Please give any suggestion. thanks...!!!


Solution

  • Here it is a solution.

    Here you are not providing swagger specification file.

    You have to change your swagger specification like,

    LoginModel:
        - type: object
          properties:
            username :
              type: string
              example: abc
              default: abc
            password :
              type: string
              example: 123
              default: 123
    

    Now you have to create a custom template for pojo.mustache (you will found this in swagger swagger-codegen in recource folder).

    you have to modify pojo.mustache here.

    {{#isPrimitiveType}} 
      private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};
    {{/isPrimitiveType}}
    {{^isPrimitiveType}}
      private {{{datatypeWithEnum}}} {{name}} = new {{{datatypeWithEnum}}}();
    {{/isPrimitiveType}}
    

    after modifing this you will achive codegen like you want...!

    i hope it will help...! thanks.