Skip to content

Commit

Permalink
fix: detect methods annotated with ArchTest as ArchUnit tests (#371)
Browse files Browse the repository at this point in the history
ArchUnit tests can be declared as fields or as methods: also check for
methods. Added missing AnalyzeClasses annotation on sample test
  • Loading branch information
gtoison authored Nov 8, 2022
1 parent 12331f9 commit 715a8da
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
* so we should keep its footprint minimal.
*/
public class JavaAssistClass extends AbstractJavaClass {
private static final String ARCHUNIT_ARCHTEST = "com.tngtech.archunit.junit.ArchTest";

private final String[] imports;
private final boolean isATest;
private final String className;
Expand Down Expand Up @@ -299,7 +301,14 @@ public boolean apply(CtClass input) {

private boolean hasArchUnitTests(CtClass classReference) {
for (CtField field : classReference.getDeclaredFields()) {
if (field.hasAnnotation("com.tngtech.archunit.junit.ArchTest")) {
if (field.hasAnnotation(ARCHUNIT_ARCHTEST)) {
return true;
}
}

// getMethods() returns the non-private methods
for (CtMethod ctMethod : classReference.getDeclaredMethods()) {
if (ctMethod.hasAnnotation(ARCHUNIT_ARCHTEST)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Infinitest, a Continuous Test Runner.
*
* Copyright (C) 2010-2013
* "Ben Rady" <benrady@gmail.com>,
* "Rod Coffin" <rfciii@gmail.com>,
* "Ryan Breidenbach" <ryan.breidenbach@gmail.com>
* "David Gageot" <david@gageot.net>, et al.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.fakeco.fakeproduct;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import static com.tngtech.archunit.library.GeneralCodingRules.ACCESS_STANDARD_STREAMS;

import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;

@AnalyzeClasses(packages = "com.fakeco.fakeproduct")
public class JUnit5ArchUnitMethodTest {

@ArchTest
private void no_access_to_standard_streams_as_method() {
noClasses().should(ACCESS_STANDARD_STREAMS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
*/
package com.fakeco.fakeproduct;

import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;

import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS;

@AnalyzeClasses(packages = "com.fakeco.fakeproduct")
public class JUnit5ArchUnitTest {

@ArchTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import com.fakeco.fakeproduct.FieldAnnotation;
import com.fakeco.fakeproduct.InvisibleClassAnnotation;
import com.fakeco.fakeproduct.InvisibleParameterAnnotation;
import com.fakeco.fakeproduct.JUnit5ArchUnitMethodTest;
import com.fakeco.fakeproduct.JUnit5ArchUnitTest;
import com.fakeco.fakeproduct.JUnit5CompositeAnnotationTest;
import com.fakeco.fakeproduct.JUnit5ParameterizedTest;
Expand Down Expand Up @@ -159,6 +160,7 @@ private String[] dependenciesOf(Class<?> dependingClass) {
JUnit5Testable.class,
JUnit5TestableSubclass.class,
JUnit5ArchUnitTest.class,
JUnit5ArchUnitMethodTest.class,
})
void shouldSupportTestClasses(Class<?> testClass) throws NotFoundException {
ClassPool classPool = classPoolUtil.getClassPool();
Expand Down

0 comments on commit 715a8da

Please sign in to comment.