Skip to content
Advertisement

Tag: junit

Trying to mock IntConsumer with Mockito fails

I’m trying to mock IntConsumer: and it fails on the ‘verify’ with below error code: Method threw ‘org.mockito.exceptions.base.MockitoException’ exception. Cannot evaluate $java.util.function.IntConsumer$$EnhancerByMockitoWithCGLIB$$3ee084c4.toString() Answer You need to tell Mockito what method it is supposed to verify on the IntConsumer mock. Your verification code should look something like: See for example the tutorial at Baeldung.

Ignoring only embedded member in AssertJ

I have two classes to test with JUnit/Mockito: Then in my test class, using AssertJ: The problem is that the name sk is in both classes, and I need to ignore it only in ClassA, not in ClassB. Is this possible in AssertJ? Answer According to the ignoringFields javadoc: Nested fields can be specified like this: home.address.street. …so with your

How to tests and mock mapstruct converter?

I’m using MapStruct framework on my java Gradle project, and it works perfectly but I just want to test : MapStruct generated sources (converter) service class (calls MapStruct’s converter) I have tried to use another topic to do this but it is not working for me. This is my MapStruct interface : This is my service class : When I’m

JUnit: assert collection contains an element with a certain property

I want to assert (using JUnit and not any other dependencies like hamcrest) if a collection contains an element with a certain property value). I found this question How do I assert an Iterable contains elements with a certain property? using harmcrest. One option is to override the equals method to check only the property that I want to compare,

UnsatisfiedDependencyException: Error creating bean with name ‘repository.BookRepositoryTest’ in Junit Test

I am writing a junit test for spring data repository. But i can’t autowire repository interface. Program is running if i run main method and spring can detect all repositories,services,entities but if i run a test it gives this error : UnsatisfiedDependencyException: Error creating bean with name ‘repository.BookRepositoryTest’: Unsatisfied dependency expressed through field ‘bookRepository’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying

JUnit5: Specify order of execution of multiple @Nested classes

Does Junit5 give any guarantees regarding execution order of @Nested classes defined within the same parent? With a trivial example I made, I notice the tests are executed in reverse order of declaration, but this behavior is neither documented (or is it?) nor consistent with how @Test annotated methods are ordered by default. It’s also quite annoying, because obviously I’d

Advertisement