@When("^I create json string from object and write to file$") public void i_create_json_string_from_object_and_write_to_file() throws Throwable { Employee employee = new Employee(); employee.setId(1); employee.setFirstName("Pradeep"); employee.setLastName("Kumar"); employee.setRoles(Arrays.asList("ADMIN", "MANAGER")); Gson gson = new GsonBuilder().setPrettyPrinting().create(); FileOutputStream fout = new FileOutputStream("output.json"); fout.write(gson.toJson(employee).getBytes()); fout.flush(); fout.close(); }