Intellij Idea使用Junit测试“移动重构”

时间:2022-12-22 05:08:11

Seems when I make "move" refactoring all my junit tests lays on its old place. Often I tests "package" visible classes, so they becomes invisible, if SUT moves to another package.

似乎当我进行“移动”重构我所有的junit测试都在它的旧地方。我经常测试“包”可见类,因此如果SUT移动到另一个包,它们将变得不可见。

Do you move tests by hand?

你手动移动测试吗?

2 个解决方案

#1


I have 4 options for you:

我有4个选项:

  1. Go to the "Package" view in the left, select both files, and then hit F6. It should move them both to the right place.

    转到左侧的“包”视图,选择这两个文件,然后按F6。它应该将它们移动到正确的位置。

  2. Make the class public temporarily, before you do your refactor, and switch back afterwards.

    在进行重构之前暂时将该类公开,然后再切换回来。

  3. Try moving the test first. I seem to remember that avoids breaking any of the dependencies.

    尝试先移动测试。我似乎记得避免破坏任何依赖。

  4. There is a plugin (I think it's toggleTest or unitTest-- I had both of them installed) that patches the Move Refactor to also bring the test with it. Worked great. Unfortunately it looks like these may not work with the latest IDEA.

    有一个插件(我认为它是toggleTest或unitTest--我已经安装了它们)修补了Move Refactor也带来了测试。工作得很好。不幸的是,看起来这些可能不适用于最新的IDEA。

#2


The behaviour you describe is perfectly normal.

你描述的行为是完全正常的。

src/package1/A.java
test/package1/ATest.java

In your ATest.java there's an import package1.A;.
After your refactored, it looks like this:

在您的ATest.java中有一个import package1.A;。重构后,它看起来像这样:

src/package2/A.java
test/package1/ATest.java

The test code stayed where he was. You did not moved the test code, but your source code. It should not affect any other folders (like in your example).
The reference in the ATest.java must now be import package2.A;. Otherwise, the refactoring went wrong.

测试代码保持原样。您没有移动测试代码,而是移动了源代码。它不应该影响任何其他文件夹(如在您的示例中)。 ATest.java中的引用现在必须是import package2.A ;.否则,重构出错了。

Nontheless, your tests should work, even if they are in a different directory. That's because the import was changed by the refactoring method.

尽管如此,您的测试应该可以正常工作,即使它们位于不同的目录中。那是因为重构方法改变了导入。

If you want to clean up your folder structure, you have to manually rename the package test/package1 to test/package2 (I know, the package is package1 and package2 but I want to strengthen the focus on the folder structure.

如果要清理文件夹结构,则必须手动将包test / package1重命名为test / package2(我知道,包是package1和package2,但我想加强对文件夹结构的关注。

I hope I could help you!

我希望我能帮到你!

#1


I have 4 options for you:

我有4个选项:

  1. Go to the "Package" view in the left, select both files, and then hit F6. It should move them both to the right place.

    转到左侧的“包”视图,选择这两个文件,然后按F6。它应该将它们移动到正确的位置。

  2. Make the class public temporarily, before you do your refactor, and switch back afterwards.

    在进行重构之前暂时将该类公开,然后再切换回来。

  3. Try moving the test first. I seem to remember that avoids breaking any of the dependencies.

    尝试先移动测试。我似乎记得避免破坏任何依赖。

  4. There is a plugin (I think it's toggleTest or unitTest-- I had both of them installed) that patches the Move Refactor to also bring the test with it. Worked great. Unfortunately it looks like these may not work with the latest IDEA.

    有一个插件(我认为它是toggleTest或unitTest--我已经安装了它们)修补了Move Refactor也带来了测试。工作得很好。不幸的是,看起来这些可能不适用于最新的IDEA。

#2


The behaviour you describe is perfectly normal.

你描述的行为是完全正常的。

src/package1/A.java
test/package1/ATest.java

In your ATest.java there's an import package1.A;.
After your refactored, it looks like this:

在您的ATest.java中有一个import package1.A;。重构后,它看起来像这样:

src/package2/A.java
test/package1/ATest.java

The test code stayed where he was. You did not moved the test code, but your source code. It should not affect any other folders (like in your example).
The reference in the ATest.java must now be import package2.A;. Otherwise, the refactoring went wrong.

测试代码保持原样。您没有移动测试代码,而是移动了源代码。它不应该影响任何其他文件夹(如在您的示例中)。 ATest.java中的引用现在必须是import package2.A ;.否则,重构出错了。

Nontheless, your tests should work, even if they are in a different directory. That's because the import was changed by the refactoring method.

尽管如此,您的测试应该可以正常工作,即使它们位于不同的目录中。那是因为重构方法改变了导入。

If you want to clean up your folder structure, you have to manually rename the package test/package1 to test/package2 (I know, the package is package1 and package2 but I want to strengthen the focus on the folder structure.

如果要清理文件夹结构,则必须手动将包test / package1重命名为test / package2(我知道,包是package1和package2,但我想加强对文件夹结构的关注。

I hope I could help you!

我希望我能帮到你!