swagger-api / swagger-api/swagger-codegen

[JAVA]null value in unit test case code

Open
#12,494 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature List: API clients Feature: Codegen pull-request-welcome
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

issue in English
Summary

I try to generate code by following command,

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
  -i https://petstore.swagger.io/v2/swagger.json \
  -l java \
  -o samples/client/petstore/java

When it done, I open PetApiTest.java, then I saw a piece of test case code,

@Test
public void addPetTest() throws Exception {
    Pet body = null;
    api.addPet(body);

    // TODO: test validations
}

Now, my issue is that I hope the body value shouldn't be null, and I hope the TODO should be completed.

Swagger-codegen version

2.4.43

Suggest a fix/enhancement

It is expected that the unit test code is complete, that is, it can create and initialize different objects, and make assertions.

Additional information: I'm working in a test technology team that provide technology for test engineer. Due to the rise of AI technology, especially the LLM like ChatGPT, my manager wants to introduce the technology to resolve some problems. Now he wants to input a JSON file written in OpenAPI Specification like swagger.json to a tool, then hope that the tool can produce some request data which can be used for calling RESTful API.

Okay. That's all, thank you.

issue in Chinese

Because I'm Chinese, I remain my Chinese issue.

问题概述

我尝试按以下命令生成代码,

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
  -i https://petstore.swagger.io/v2/swagger.json \
  -l java \
  -o samples/client/petstore/java

生成后,我打开PetApiTest.java,看到其中一个测试用例:

@Test
public void addPetTest() throws Exception {
    Pet body = null;
    api.addPet(body);

    // TODO: test validations
}

现在问题是,我希望body值不是null,并且希望TODO代码已经自动写好了。

Swagger-codegen version

2.4.43

Suggest a fix/enhancement

希望单元测试代码是完全的,也就是能够创建和初始化不同的对象,并且做出断言。

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the documented swagger-codegen command and inspecting the generated PetApiTest.java, especially addPetTest. Trace the Java test-generation template and determine how request objects and assertions could be derived from the OpenAPI input. Done would mean generated tests initialize usable request data, perform meaningful assertions, and no longer leave this TODO unresolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
testing, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.