跳到正文
Skillpedia
测试40k · #7256

java-junit

它能做什么

这个工具可以帮你使用 JUnit 5 为 Java 代码编写测试。它会讲解测试常用功能的最佳实践,也支持使用各种输入数据进行测试。

适用场景

  • ✓当你想确保 Java 代码运行正确时。
  • ✓当你在学习为项目编写自动化测试时。
  • ✓当你想用不同的输入值测试代码,又不想重复编写代码时。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/java-junit/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/java-junit/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我怎样为求和函数写一个测试,检查 2+2 是否等于 4?”

每周 5 个新技能,1 分钟读完。

工作原理

这个工具会讲解如何组织测试,如何使用特定的 JUnit 注解来设置和运行测试,以及如何验证是否得到了预期结果。它还会演示如何使用不同的数据源,用多种场景来测试你的代码。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

JUnit 5+ Best Practices

Your goal is to help me write effective unit tests with JUnit 5, covering both standard and data-driven testing approaches.

Project Setup

  • Use a standard Maven or Gradle project structure.
  • Place test source code in src/test/java.
  • Include dependencies for junit-jupiter-api, junit-jupiter-engine, and junit-jupiter-params for parameterized tests.
  • Use build tool commands to run tests: mvn test or gradle test.

Test Structure

  • Test classes should have a Test suffix, e.g., CalculatorTest for a Calculator class.
  • Use @Test for test methods.
  • Follow the Arrange-Act-Assert (AAA) pattern.
  • Name tests using a descriptive convention, like methodName_should_expectedBehavior_when_scenario.
  • Use @BeforeEach and @AfterEach for per-test setup and teardown.
  • Use @BeforeAll and @AfterAll for per-class setup and teardown (must be static methods).
  • Use @DisplayName to provide a human-readable name for test classes and methods.

Standard Tests

  • Keep tests focused on a single behavior.
  • Avoid testing multiple conditions in one test method.
  • Make tests independent and idempotent (can run in any order).
  • Avoid test interdependencies.

Data-Driven (Parameterized) Tests

  • Use @ParameterizedTest to mark a method as a parameterized test.
  • Use @ValueSource for simple literal values (strings, ints, etc.).
  • Use @MethodSource to refer to a factory method that provides test arguments as a Stream, Collection, etc.
  • Use @CsvSource for inline comma-separated values.
  • Use @CsvFileSource to use a CSV file from the classpath.
  • Use @EnumSource to use enum constants.

Assertions

  • Use the static methods from org.junit.jupiter.api.Assertions (e.g., assertEquals, assertTrue, assertNotNull).
  • For more fluent and readable assertions, consider using a library like AssertJ (assertThat(...).is...).
  • Use assertThrows or assertDoesNotThrow to test for exceptions.
  • Group related assertions with assertAll to ensure all assertions are checked before the test fails.
  • Use descriptive messages in assertions to provide clarity on failure.

Mocking and Isolation

  • Use a mocking framework like Mockito to create mock objects for dependencies.
  • Use @Mock and @InjectMocks annotations from Mockito to simplify mock creation and injection.
  • Use interfaces to facilitate mocking.

Test Organization

  • Group tests by feature or component using packages.
  • Use @Tag to categorize tests (e.g., @Tag("fast"), @Tag("integration")).
  • Use @TestMethodOrder(MethodOrderer.OrderAnnotation.class) and @Order to control test execution order when strictly necessary.
  • Use @Disabled to temporarily skip a test method or class, providing a reason.
  • Use @Nested to group tests in a nested inner class for better organization and structure.
常见问题

什么是单元测试?

单元测试是一种独立检查代码中一小块内容(通常是一个函数或方法)的测试,用来确保它按预期工作。

什么是“数据驱动”测试?

这种测试会用不同的输入和输出数据,多次运行同样的测试逻辑。这样就能避免重复编写代码。

使用这个工具需要很懂 Java 吗?

具备基础的 Java 知识会有帮助,但这个工具的设计目标就是,即使是初学者也能清楚理解测试概念。

详情

作者的原始描述

Get best practices for JUnit 5 unit testing, including data-driven tests

repo: github/awesome-copilot

热度

GitHub 星标
40k
排名
#7256
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能