I have a BeerRepository interface that extends JPARepository<Beer,UUID>. This interface contains this method.
Page<Beer> findAllByBeerName(String beerName, Pageable pageable);
In a controller handler method, I have this.
Page<Beer> pagedResult = beerRepository.findAllByBeerName(beer.getBeerName(),
createPageRequest(0,10,Sort.Direction.DESC,"beerName"));
I am trying to unit test the controller handler method.
The #BeforeEach method is this.
. . .
Page<Beer> pagedResponse;
#BeforeEach
void setUp() {
beerList = new ArrayList<Beer>();
beerList.add(Beer.builder().build());
beerList.add(Beer.builder().build());
pagedResponse = new PageImpl(beerList);
mockMvc = MockMvcBuilders
.standaloneSetup(controller)
.build();
}
My #Test method is this.
#Test
void processFindFormReturnMany() throws Exception{
when(beerRepository.findAllByBeerName(anyString(), PageRequest.of(0,
10,Sort.Direction.DESC,"beerName"))).thenReturn(pagedResponse);
mockMvc.perform(get("/beers"))
.andExpect(status().isOk())
.andExpect(view().name("beers/beerList"))
.andExpect(model().attribute("selections", hasSize(2)));
}
On running the test, I'm getting
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at
guru.sfg.brewery.web.controllers.BeerControllerTest.
processFindFormReturnMany
(BeerControllerTest.java:67)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
Any help on this will be highly appreciated.
The problem is: you cannot mix Mockito matchers with real values while mocking with when(...). See docs.
In your case the following should work correctly:
when(beerRepository.findAllByBeerName(anyString(), eq(PageRequest.of(0,
10,Sort.Direction.DESC,"beerName")))).thenReturn(pagedResponse);
or
when(beerRepository.findAllByBeerName(anyString(), any(PageRequest.class)).thenReturn(pagedResponse);
Finally, you can use the real expected string instead of anyString() and leave PageRequest.of(...) as is. It should work too (assuming that they were configured correctly).
You need to set mock class for param when you mock any method
when(beerRepository.findAllByBeerName(anyString(), any(Pageable.class)).thenReturn(pagedResponse);
Related
I am comparing two objects emsResponse and expectedEMSResponse of the same class EMSResponse in a Junit test case but the assertThat(...) test case passes despite the objects being unequal. When I try a normal equals on the two objects the output is false but the assertThat(expectedEMSResponse==eMSResponse) still passes rather than failing. Please advise.
#NoArgsConstructor
#AllArgsConstructor
#Getter
#Setter
#ToString
#EqualsAndHashCode
public class RestconfRes {
private String response;
private int httpStatusCode;
}
#Getter
#Setter
#ToString
#EqualsAndHashCode(callSuper = true)
public class EMSResponse extends RestconfRes {
private EMSOutput output;
private EMSErrors errors;
private String message;
public EMSOutput getOutput() {
if (output == null) {
output = new EMSOutput();
}
return output;
}
}
// Junit test case
#Test
public void processJMSRequestForHappyPathTest() throws Exception {
// test case logic here
// mock logic here
EMSResponse emsResponse = processor.processJMSRequest(request);
System.out.println(" expectedEMSResponse ..." + EMSResponse.toString());
System.out.println(" processJMSRequestForHappyPathTest expectedEMSResponse ...:" + expectedEMSResponse.toString());
System.out.println("check if i am equal..."+ expectedEMSResponse.equals(emsResponse));
assertThat(expectedEMSResponse==eMSResponse);
}
Output:
expectedEMSResponse ...EMSResponse(output=EMSoutput(deviceName=null, timestamp=null, status=failure, transId=null, completionStatus=null, serviceId=null, contentProviderName=null, interfaceName=null), errors=EMSerrors(errorType=INTERNAL_FAILURE, errorTag=A41_INTERNAL_EXCEPTION, errorMessage=An internal error occurred within A41. Please contact ASG.), message=null)
emsResponse ...EMSResponse(output=EMSoutput(deviceName=device_3, timestamp=2020-07-15T16:32:31.881000, status=configuring, transId=66427-d8b5-489f-9f8f, completionStatus=in-progress, serviceId=null, contentProviderName=null, interfaceName=null), errors=null, message=null)
check if i am equal...false
Updated second sub question to this
I changed the call to:
assertTrue(expectedEMSResponse==emsResponse);
// gives a java.lang.AssertionError and
System.out.println("check if i am equal..."+ expectedEMSResponse.equals(emsResponse)); gives output
check if i am equal...false
// this test case passes Ok assertThat(emsResponse.getOutput().getTransId()).isEqualTo(expectedEMSResponse.getOutput().getTransId());
When I try printing the details of the emsResponse and expecteEMSResponse classes through toString() I get the below output:
// gives only a bean as an output
emsResponse.toString():com.myorg.myapp.interfaces.dto.emsResponse#0 bean
//gives the complete object signature
expectedemsResponse.toString():emsResponse(output=emsOutput(deviceName=device_3,
timestamp=2020-07-15T16:32:31.881000, status=configuring, transId=hello-there-489f-9f8f-abcd,
completionStatus=in-progress, serviceId=null, contentProviderName=null, interfaceName=null), errors=null, message=null)
You are probably using AssertJ because I don't think JUnit 4 or Hamcrest have a single argument version of assertThat(), and JUnit 5 doesn't have such method.
The way AssertJ assertThat() works is that the first argument of the call is the actual value that you want to perform assertions on. This returns an assertion object, which provides you a number of assertion methods.
When you call assertThat(expectedEMSResponse==eMSResponse), AssertJ just returns an assertion object for boolean type and nothing else happens.
The correct way to use it would be:
assertThat(eMSResponse).isEqualTo(expectedEMSResponse);
Now it returns an assertion object for the first argument, and then performs the isEqualTo() assertion on that one. The assertThat() call only works as an entry point to different assertions that can be performed on the first argument.
I don't think you are using assertThat correctly. Looks like you need to pass additional arguments as well. These are the method definitions:
assertThat(T actual, Matcher<? super T> matcher)
assertThat(String reason, T actual, Matcher<? super T> matcher)
Also as per the documentation, it is deprecated. Might I suggest that you use assertEquals() instead?
The documentation: https://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThat
This does not fail because you are not using junit correctly:
#Test
public void processJMSRequestForHappyPathTest() throws Exception {
// test case logic here
// mock logic here
EMSResponse emsResponse = processor.processJMSRequest(request);
System.out.println(" expectedEMSResponse ..." + EMSResponse.toString());
System.out.println(" processJMSRequestForHappyPathTest expectedEMSResponse ...:" + expectedEMSResponse.toString());
System.out.println("check if i am equal..."+ expectedEMSResponse.equals(emsResponse));
assertThat(expectedEMSResponse==eMSResponse);
}
You should have:
assertTrue(expectedEMSResponse==eMSResponse);
assertEquals(expectedEMSResponse, eMSResponse);
Or using assertThat with assertj:
assertThat(eMSResponse).isEqualTo(expectedEMSResponse);
This should help you understand:
junit 5: https://junit.org/junit5/docs/current/user-guide/
junit 4: https://www.baeldung.com/junit-assertions
I need to mock the following Model Mapper Strict strategy configuration with the mockito.
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
I tried the following in my test method but i am getting Null Pointer Exception.
#Test
public void mockModelMapper(){
when(modelmapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT))
.thenReturn(modelmapper.getConfiguration());
}
Thanks in advance.
As mentioned by #DCTID, you can't chain the mocks. You need to split each method call and inject a mock for the object returned by getConfiguration().
#Test
public void mockModelMapper(){
// Inject the configuration mock
Configuration configurationMock = mock(Configuration.class);
when(configurationMock.setMatchingStrategy(MatchingStragegies.STRICT)
.thenReturn(configuraitonMock);
when(modelmapper.getConfiguration()).thenReturn(configurationMock);
}
I am trying to use PowerMockito to mock the creation of the java.net.URL class in my code that I'm testing. Basically, I want to prevent the real HTTP request from occurring and instead 1) check the data when the request is made and 2) supply my own test data back on a mocked response. This is what I'm trying:
#RunWith(PowerMockRunner.class)
#PrepareForTest({ URL.class, MockedHttpConnection.class })
public class Test {
URL mockedURL = PowerMockito.mock(URL.class);
MockedHttpConnection mockedConnection = PowerMockito.mock(MockedHttpConnection.class);
...
PowerMockito.whenNew(URL.class).withParameterTypes(String.class).withArguments("MyURLString").thenReturn(mockedURL);
PowerMockito.when(mockedURL.openConnection()).thenReturn(mockedConnection);
...
}
The code that I want to test looks like this:
URL wlInvokeUrl = new URL(wlInvokeUrlString);
connection = (HttpURLConnection) wlInvokeUrl.openConnection();
Earlier in my test scenario I mock the wlInvokeUrlString to match "MyURLString". I've also tried using various other forms of the whenNew line, trying to inject the mock. No matter what I try, it never intercepts the constructor. All I want to do is "catch" the call to openConnection() and have it return my mocked HTTP connection instead of the real one.
I have mocked other classes ahead of this one in the same script and these are working as expected. Either I need a second pair of eyes (probably true) or there is something unique about the URL class. I did notice that if I use "whenNew(URL.class).withAnyArguments()" and change the "thenReturn" to "thenAnswer" I could get it to trigger. Only problem is I never get the URL call for my code. What I see is an invocation of the 3-argument constructor for URL.class with all nulls for the parameters. Could it be this class is from the Java runtime and is bootstrapped by the test runner? Any help is much appreciated.
It's a common mistake when use PowerMockito.whenNew.
Note that you must prepare the class creating the new instance of MyClass for test, not the MyClass itself. E.g. if the class doing new MyClass() is called X then you'd have to do #PrepareForTest(X.class) in order for whenNew to work
From Powermock wiki
So, you need a bit change your test and add to #PrepareForTesta class which create a new instance of URLlike:
#RunWith(PowerMockRunner.class)
#PrepareForTest({ URL.class, MockedHttpConnection.class , ConnectionUser.class})
public class URLTest {
public class URLTest {
private ConnectionUser connectionUser;
#Before
public void setUp() throws Exception {
connectionUser = new ConnectionUser();
}
#Test
public void testName() throws Exception {
URL mockedURL = PowerMockito.mock(URL.class);
MockedHttpConnection mockedConnection = PowerMockito.mock(MockedHttpConnection.class);
PowerMockito.whenNew(URL.class).withParameterTypes(String.class).withArguments("MyURLString").thenReturn(mockedURL);
PowerMockito.when(mockedURL.openConnection()).thenReturn(mockedConnection);
connectionUser.open();
assertEquals(mockedConnection, connectionUser.getConnection());
}
}
where:
public class ConnectionUser {
private String wlInvokeUrlString = "MyURLString";
private HttpURLConnection connection;
public void open() throws IOException {
URL wlInvokeUrl = new URL(wlInvokeUrlString);
connection = (HttpURLConnection) wlInvokeUrl.openConnection();
}
public HttpURLConnection getConnection() {
return connection;
}
}
I'm not sure the difference between .withParameterTypes(x) and .withArguments(x) but I believe you need to set it up as follows for your code to work. Give it a try and let me know if this doesn't help.
PowerMockito.when(mockedURL.openConnection()).thenReturn(mockedConnection);
PowerMockito.whenNew(URL.class).withArguments(Mockito.anyString()).thenReturn(mockedURL);
The problem is that the arguments of the real call are not matching with the expected in your mock.
PowerMockito.whenNew(URL.class).withParameterTypes(String.class).withArguments("MyURLString").thenReturn(mockedURL) will return mockedURL only the call is new URL("MyURLString").
If you change it to:
PowerMockito.whenNew( URL.class ).withParameterTypes( String.class )
.withArguments( org.mockito.Matchers.any( String.class ) ).thenReturn( mockedURL );
It will catch any string passed to the constructor URL(String) (even null) and return your mock.
When you tried
"whenNew(URL.class).withAnyArguments()" and change the "thenReturn" to
"thenAnswer" I could get it to trigger. Only problem is I never get
the URL call for my code. What I see is an invocation of the
3-argument constructor for URL.class with all nulls for the
parameters.
PowerMock will try to mock all constructors (org.powermock.api.mockito.internal.expectation.DelegatingToConstructorsOngoingStubbing.InvokeStubMethod at line 122) then it calls the first one (with 3 arguments) and mock its answer. But the subsequent calls will return the already mocked one because you told it to mock for any arguments. That's why you see just one call with null, null, null in your Answer.
That's my test code:
public void testApplyListWhenAddTheSameIDThenReturnDuplicateEntityException(){
MyEntity entityRCM = createMyEntity(AGE_ID, WEIGHT_ID, 0L);
entityModel.addEntity(entityRCM);
MyEntity entityOPC = createMyEntity(DIFF_AGE_ID, WEIGHT_ID, 0L);
EntityCreate create = new EntityCreate(entityOPC);
List<EntityChange> changeList = new ArrayList<EntityChange>();
changeList.add(create);
try {
entityModel.apply(changeList);
fail();
}catch(DuplicateEntityException e) {
PowerMockito.verifyStatic(times(20));
LogManager.error(Mockito.<Logger>anyObject(),Mockito.anyString(),Mockito.<DuplicateEntityException>anyObject());
}
}
The problem is here:
PowerMockito.verifyStatic(times(20));
LogManager.error(Mockito.<Logger>anyObject(),Mockito.anyString(),Mockito.<DuplicateEntityException>anyObject());
I want to verify a static method error in class LogManager, but how can I verify this method for twenty times but it doesn't fail.
Solved in the comments:
Did you add #PrepareForTest and mockStatic? If not, then you may have set up your matchers and static call but PowerMock never sees the actual call to mock before your test completes.
For context, PowerMock mocks static classes by intercepting the classloader and loading a replacement class that calls PowerMock-provided implementations instead of the originals. Unless you add the right preparation, PowerMock won't replace the class, so it won't count static method calls or identify the method to verify, and the test will complete before the verification call ever actually happens.
I use Jersey and I have the following Rest function which returns a JSON string when my server is deployed:
#GET
#Path("getallemployees")
#Produces("application/json")
public Response getAllEmployees() {
//building the entity object which is List<Employee>
return Response.ok(entity).build();
}
I need to develop some unit tests (not integration testing) and I want to somehow mock the HTTPRequest that invokes this method and then get the json String. The best option would be to use mockito for this.
Is there any suggestion on how to do it ?
Thanks !!
The problem is that the method returns a Response object to the caller which is deep within the framework code. It doesn't return JSON strings.
You can use Mockito, if you need to mock something inside the method itself. That should work.
But you may need to take the value returned by the method and convert it to JSON like this if you are using Jackson with Jersey.
Response response = getAllEmployees();
Object retval = response.getEntity();
try {
ObjectMapper mapper = new ObjectMapper();
// I like this formatting. You can change it.
mapper.configure(Feature.INDENT_OUTPUT, true);
mapper.configure(Feature.WRITE_ENUMS_USING_TO_STRING, true);
mapper.configure(Feature.USE_ANNOTATIONS, false);
mapper.configure(Feature.FAIL_ON_EMPTY_BEANS, false);
mapper.setSerializationInclusion(Inclusion.NON_NULL);
mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
mapper.getSerializationConfig().withSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
String json = mapper.writeValueAsString(retval);
... assert something about the string
} catch (JsonProcessingException e) {
// do something
} catch (IOException e) {
// do something
}
Some of this is guess work and speculation on my part but it may help. You could try using the Jersey Test Framework with the InMemoryTestContainerFactory:
It starts Jersey application and directly calls internal APIs to handle request created by client provided by test framework. There is no network communication involved. This containers does not support servlet and other container dependent features, but it is a perfect choice for simple unit tests.
It looks like to use it, all you need to do is extend JerseyTest and then override getTestContainerFactory() and follow the rest of the instructions, e.g.:
public class EmployeeResourceTest extends JerseyTest {
#Override
protected Application configure() {
// set up employee resource with mock dependencies etc...
return new ResourceConfig().registerInstances(employeeResource);
}
#Test
public void getAllEmployees() {
final String response = target("getallemployees").request().get(String.class);
// assert etc...
}
}
I used registerInstances instead of registerClasses in configure() as it looks like you can present a ready made Resource but set up with any mock dependencies you may want - although I haven't tried this myself.
The test class is a bit inflexible as you can only do one-time set up of dependencies in the configure() method, so it might be worth investigating using the MockitoJUnitRunner - although I'm not sure if it will work with the JerseyTest inheritance. It could allow you to do add behaviour to mocks in each #Test method, e.g.:
#Mock
private EmployeeResourceDependency dependency;
#InjectMocks
private EmployeeResource employeeResource;
// configure() as above but without mock setup up etc...
#Test
public void getAllEmployees() {
given(dependency.getEmployees()).willReturn(...);
// etc...
But like I said it might not be possible to mix them at all.