forked from TNG/ArchUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue: TNG#475 - Implement ArchRule that checks if test class reside …
…in the same package as implementation class Signed-off-by: Marcin Słowiak <marcin.slowiak.007@gmail.com>
- Loading branch information
Showing
8 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...t/java/com/tngtech/archunit/library/TestClassesPackagesAlignedWithImplementationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.tngtech.archunit.library; | ||
|
||
import com.tngtech.archunit.core.domain.JavaClasses; | ||
import com.tngtech.archunit.core.importer.ClassFileImporter; | ||
import com.tngtech.archunit.library.testclasses.packages.correct.SecondClass; | ||
import com.tngtech.archunit.library.testclasses.packages.incorrect.FirstClass; | ||
import org.junit.Test; | ||
|
||
import static com.tngtech.archunit.library.GeneralCodingRules.testClassesShouldResideInTheSamePackageAsImplementation; | ||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class TestClassesPackagesAlignedWithImplementationTest { | ||
|
||
private final JavaClasses correctClasses = new ClassFileImporter().importPackagesOf(SecondClass.class); | ||
private final JavaClasses incorrectClasses = new ClassFileImporter().importPackagesOf(FirstClass.class); | ||
|
||
@Test | ||
public void should_fail_when_test_class_reside_in_different_package_as_implementation() { | ||
// given | ||
final String correctPackage = "com.tngtech.archunit.library.testclasses.packages.incorrect"; | ||
final String incorrectTestClass = "com.tngtech.archunit.library.testclasses.packages.incorrect.dir.FirstClassTest"; | ||
final String expectedErrorMessage = "Test class " + incorrectTestClass + " is in the wrong package. Should be inside " + correctPackage + " package"; | ||
|
||
// when & then | ||
assertThatThrownBy(() -> testClassesShouldResideInTheSamePackageAsImplementation().check(incorrectClasses)) | ||
.isInstanceOf(AssertionError.class) | ||
.hasMessageContaining(expectedErrorMessage); | ||
} | ||
|
||
@Test | ||
public void should_fail_when_test_class_reside_in_different_package_as_implementation_when_have_custom_suffix() { | ||
// given | ||
final String correctPackage = "com.tngtech.archunit.library.testclasses.packages.incorrect"; | ||
final String incorrectTestClass = "com.tngtech.archunit.library.testclasses.packages.incorrect.dir.FirstClassTestingScenario"; | ||
final String expectedErrorMessage = "Test class " + incorrectTestClass + " is in the wrong package. Should be inside " + correctPackage + " package"; | ||
final String testClassesSuffix = "TestingScenario"; | ||
|
||
// when & then | ||
assertThatThrownBy(() -> testClassesShouldResideInTheSamePackageAsImplementation(testClassesSuffix).check(incorrectClasses)) | ||
.isInstanceOf(AssertionError.class) | ||
.hasMessageContaining(expectedErrorMessage); | ||
} | ||
|
||
@Test | ||
public void should_not_fail_when_test_class_and_implementation_class_reside_in_the_same_package() { | ||
// when & then | ||
assertThatNoException().isThrownBy(() -> testClassesShouldResideInTheSamePackageAsImplementation().check(correctClasses)); | ||
} | ||
|
||
@Test | ||
public void should_not_fail_when_test_class_and_implementation_class_reside_in_the_same_package_with_custom_suffix() { | ||
// given | ||
final String testClassesSuffix = "TestingScenario"; | ||
|
||
// when & then | ||
assertThatNoException().isThrownBy(() -> testClassesShouldResideInTheSamePackageAsImplementation(testClassesSuffix).check(correctClasses)); | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
.../src/test/java/com/tngtech/archunit/library/testclasses/packages/correct/SecondClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tngtech.archunit.library.testclasses.packages.correct; | ||
|
||
public class SecondClass { | ||
} |
4 changes: 4 additions & 0 deletions
4
.../test/java/com/tngtech/archunit/library/testclasses/packages/correct/SecondClassTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tngtech.archunit.library.testclasses.packages.correct; | ||
|
||
class SecondClassTest { | ||
} |
4 changes: 4 additions & 0 deletions
4
...com/tngtech/archunit/library/testclasses/packages/correct/SecondClassTestingScenario.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tngtech.archunit.library.testclasses.packages.correct; | ||
|
||
class SecondClassTestingScenario { | ||
} |
4 changes: 4 additions & 0 deletions
4
...src/test/java/com/tngtech/archunit/library/testclasses/packages/incorrect/FirstClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tngtech.archunit.library.testclasses.packages.incorrect; | ||
|
||
public class FirstClass { | ||
} |
4 changes: 4 additions & 0 deletions
4
.../java/com/tngtech/archunit/library/testclasses/packages/incorrect/dir/FirstClassTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tngtech.archunit.library.testclasses.packages.incorrect.dir; | ||
|
||
class FirstClassTest { | ||
} |
4 changes: 4 additions & 0 deletions
4
...ngtech/archunit/library/testclasses/packages/incorrect/dir/FirstClassTestingScenario.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tngtech.archunit.library.testclasses.packages.incorrect.dir; | ||
|
||
class FirstClassTestingScenario { | ||
} |