Replies: 3 comments 4 replies
-
Maybe I am getting it wrong but methods ()
.that().areStatic()
.shouldNot().call(methodsThat(areNamed("mockedMethodName") ; I am trying to open a pr soon but my personal live is quite busy |
Beta Was this translation helpful? Give feedback.
-
I'm not sure I fully get the question. ArchUnit will only detect static properties of code, not runtime properties. So it will not know that there is a mock proxy injected at runtime or something like that. It can only detect what you can also see on the screen, i.e. a field is annotated with Can you outline a little more what you actually want to achieve? What do you mean by "When doing |
Beta Was this translation helpful? Give feedback.
-
As far as I see the call should be found, maybe the test is not in the
scope that you analyze because not only the class that is accessed needs to
be in the scan but also the accessor
…On Tue, 30 Aug 2022, 09:54 liechtir ***@***.***> wrote:
Hi guys, thanks for your answers. Sorry that I was not accurate enough.
Basically, I want to find dead code. To put it simplified for this thread
here, I want all methods to be called at least once:
@test
public void deadMethods() {
JavaClasses importedClasses = new ClassFileImporter().importPackages("my.packages");
ArchRule rule = ArchRuleDefinition.methods().should(new ArchCondition<JavaMethod>("be called at least once") {
@OverRide
public void check(JavaMethod method, ConditionEvents events) {
if (method.getAccessesToSelf().isEmpty()) {
events.add(SimpleConditionEvent.violated(method, method.getDescription()));
}
}
});
rule.check(importedClasses);
}
For methods that are called in a mocked environment, like below
@RunWith(MockitoJUnitRunner.class)
public class MyTest {
@Injectmocks
MyService testee;
@test
public void testSomething(){
testee.doSomething();
}
}
The call to 'doSomething()' is not tracked and not found in the rule. A
violation is added even though the code is called. So this creates a
false-positive.
Yes, I agree, for code that is called from tests only, we should remove
the code from production code and move it into the test setup or the like.
However, this should be a different rules for later on, now focus on real
dead code that is never called.
Thanks!
—
Reply to this email directly, view it on GitHub
<#948 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIETDIYQVJUACFVQE2QYZVTV3W45DANCNFSM57PFUEWA>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Hi
we are using Mockito in our tests to mock certain functionality.
Mockito also offers to @Injectmocks, which creates an instance and injects dummy mocks on all dependencies in a class.
Archunit does not find method references/calls of such objects.
In example:
When doing
ArchRuleDefinition.methods()
, the call todoSomething
is not there.Any idea how to combine ArchUnit with Mockito?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions