I am new in unit testing and I am stuck with this error. I wonder if anyone here got an insight as to how I can resolve this issue.
So this is a snippet of my unit test.
#Test
void shouldFindByStatus() {
when(repository.findByBookingStatus(anyString(),Sort.by("id").descending())).thenReturn(getBookingEntityForFilterByStatus());
when(mapper.toDomain(any(List<BookingEntity.class))).thenReturn(getBookingFilterResponseDTO());
List<BookingFilterResponseDTO> underTest = adapter.findByBookingStatus("CANCELLED");
assertNotNull(underTest);
}
The error I encountered is this part:
(any(List<BookingEntity.class)))
It says that "List" is an expected expression. Now the problem is if I remove the List, it will throw a different error since I declared it as List on my mapper class.
This is my mapper class.
List<BookingFilterResponseDTO> toDomain(List<BookingEntity> bookingEntity);
If anyone here got an idea as to how I can resolve the said issue. I would highly and sincerely appreciate it. Thanks!
Related
Actually, I am facing below exception
ERROR : XML Read or Write is not done properly.
org.springframework.oxm.UncategorizedMappingException: Unknown JAXB exception; nested exception is javax.xml.bind.JAXBException: my_ClassName nor any of its super class is known to this context.
While making request that is throwing exception XMLMappingException. There is no code change in existing I just added new wsdl(converted to jar in proper place)but facing issue.
If anyone knows answer this question.
I tried the stack overflow solutions regarding this issue, solutions are not matching to my problem statement.
I want to mock the restTemplate.exchange when I tried the following code to do that.
Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(clientEndpoint.getUrl()),ArgumentMatchers.eq(HttpMethod.POST),
ArgumentMatchers.eq(new HttpEntity<>(ArgumentMatchers.any(String.class),ArgumentMatchers.eq(headers))), ArgumentMatchers.eq(Map.class))).thenReturn(rEntity);
I got the following error,
How can I resolve this issue?
Thanks in advance!
I solved the issue, it seems there is no ArgumentMatchers.eq then followed by a new instance, I changed the code like this
Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(clientEndpoint.getUrl()),ArgumentMatchers.eq(HttpMethod.POST),
new HttpEntity<>(ArgumentMatchers.any(String.class),ArgumentMatchers.eq(headers)), ArgumentMatchers.eq(Map.class))).thenReturn(rEntity);
and it works to me now
I have this bizarre situation with TestNG and hoping someone could help.
I'm trying to test for an IllegalArgumentException, essentially trying to block anything malicious that might end being passed into a setter on a model.
The test that I have annotated with the expected exception compiles and runs fine, but it passes despite me not throwing any IllegalArgumentException in the code and I am at a loss as to why.
public class CarTest extends ModelTest {
#Test(expectedExceptions = InvalidArgumentException.class)
public void willThrowInvalidArgumentExceptionWhenSettingEngineToNull() throws InvalidArgumentException{
Car car = new Car();
car.setEngine(null);
}
}
This passes despite no thrown exception.
The model I am using is a groovy model so will have already a public setter for this property.
I have changed the model names but it shouldn't make any difference in this case.
Thank you in advance for your wisdom
It seems to me that car.setEngine(null); is throwing the exception. Remove the line and the test should fail or send a valid argument.
I have fixed this by deleting .idea folder in IntelliJ and reimporting my project using my pom.xml file, I can only assume TestNG had not been pulled in properly and that is why expectedExceptions was not fully recognised.
Am trying to run one test case which will update the data into DB. This is my source code of test method.
#Tested // This is class-level scope as I have different test methods.
FirstLevelClass firstLevelClass;
#Test
public void testUpdateDB(#Mocked SecondLevelClass secondLevelClass) throws Exception {
// Updated method by passing an argument.
firstLevelClass.updateDatabaseThroughSecondLevelClass(info);
new Verifications() {{
SecondLevelClass.updateDB(creds, data);
times =1;
}};
Here my intention is to verify the expected invocations to mocked methods[which recorded in expectations]. But, verifications block is giving the following exception message. If I remove times=1, then test case is getting success. That is not my desired result.Can anyone please suggest me what could be wrong in my test case.
mockit.internal.MissingInvocation: Missing 1 invocation to:
SecondLevelClass#updateDB(creds, data)
with arguments: creds, data
Caused by: Missing invocations
Updated Question :
There is one argument to updateDatabaseThroughSecondLevelClass(info), from that argument we are forming creds reference in SecondLevelClass.
Credentials creds = info.getCredentials();
But in verifications block[Which is part of FirstLevelClass] we have created locally test object.
Credentials creds = getCredsTestObject();
This is the reason why it complained about Missing invocations. Because both are two different references in two classes. Can anyone please suggest me how to handle this case.
Thanks In Advance.
It is a known issue in the integration between TestNG and JMockit: https://github.com/jmockit/jmockit1/issues/337
I'm currently stuck on these problem. when I tried using #PreAuthorize("hasRole('ROLE_USER')") it works, but not on #PreAuthorize("isAuthenticated()"). I'm trying to make it return a accessdeniedexception but it returns nullpointerexception.
does anyone have encountered these kind of issue before?
I never thought that there is a custom class that extends the SecurityRoot.class but forgot that TrustResolver.class is not pass causing the nullpointer exception. I have just set the trustResolver and it work as I wanted.