Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questions and Suggestions Regarding CAP Java Testing Documentation #1544

Open
ziyang-works opened this issue Jan 8, 2025 · 1 comment
Open

Comments

@ziyang-works
Copy link
Contributor

ziyang-works commented Jan 8, 2025

Hi, I've recently started working with CAP Java and have some questions about the Testing documentation. I hope to get some clarification here or possibly see these addressed in a future update to the documentation.

The current documentation categorizes testing into three types:

  1. Event Handler Layer Testing
  2. Service Layer Testing
  3. Integration Testing

Use Case

I have the following files:

  • UserImportService.cds: A CDS service file providing a custom action import for bulk user creation.
  • UserImportHandler.cds: A service handler file implementing the custom action logic.

I need to test the following scenarios:

  • Case 1: Validate HTTP response codes:
    • Correct JSON request → HTTP 2xx
    • Invalid JSON/missing auth → Corresponding 4xx
  • Case 2: Handle invalid request bodies:
    • Missing fields or invalid types → Expected exception and no database entries
  • Case 3: Verify field mapping:
    • Ensure input fields are correctly mapped to output values using the service handler’s mapper classes.
  • Case 4: Ensure transactional integrity:
    • If one user creation fails, no users should be created in the database.

Current Testing Approach

To cover the above scenarios, I currently employ the following three testing types:

  1. Unit Tests (...MapperTest.java)
    • Test mapping logic for Case 3 using mocks.
  2. Service Layer Tests (UserImportServiceTest.java with @SpringBootTest)
    • Validate business logic for Case 2 and Case 4 with Java entity objects as request body
  3. Integration Tests (UserImportServiceITest.java with @SpringBootTest)
    • Test end-to-end HTTP calls for Case 1 with JSON request body

Challenges and Questions

  1. Overlapping Tests:

    • Service Layer Tests and Integration Tests have overlaps (although Service Layer Tests include many more edge cases). Is there a best practice for distinguishing these types of tests?
  2. Naming Confusion:

    • File names like UserImportServiceTest.java and UserImportServiceITest.java can confuse future developers about where to add new tests. Could you suggest clearer naming conventions?
  3. Terminology in Documentation:

    • Why does the documentation refer to Service Layer test files as ...ServiceTest.java when the default handler files are named ...Handler.java? This could be confusing, especially in projects where a ...Service.java file might exist for additional business logic.
  4. Performance vs. Mocking:

    • Tests with @SpringBootTest are slower than unit tests with mocks. However, heavy mocking can make the test code complex. What is the recommended balance between these approaches?
  5. Example Projects:

    • Is there a large CAP example project demonstrating these three testing types in a structured way?

Thank you for your time! I look forward to your insights and suggestions.

@renejeglinsky
Copy link
Contributor

Hi @ziyang-works ,

thanks a lot for opening this issue. I'm on it to find an expert for those topics. We'll get back to you.

Thanks René

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants