I have a error in the test in the matcher arguments after I defend mockingt method
My Project in git: https://knik...@github.com/knikitin/PhoneShop.git
I did test when worked:
recource.src.test.java.phonesshop.web.DirectoryNavigationsControllerTest
commit 1fa8321aa1e408fad392451914cce097c7c9f77e [1fa8321]
@Test
public void updateOneNavigation_ErrorNoEntityWithID_ReturnNoContent() throws Exception {
DirectoryNavigations oneNavigation = getNewDirectoryNavigations(1111L, "test");
given(this.directoryNavigationsServiceMock.updateOneNavigation(Mockito.anyLong(),Mockito.any(DirectoryNavigations.class))
).willReturn(null);
RequestPostProcessor bearerToken = helper.bearerToken("myclientwith");
String jsonString = toJSONString(oneNavigation);
mvc.perform(MockMvcRequestBuilders.put("/directorynavigations/1111")
.contentType("application/json;charset=UTF-8")
.content(jsonString)
.with(bearerToken))
.andExpect(status().isNoContent())
;
verify(directoryNavigationsServiceMock, times(1)).updateOneNavigation(Mockito.anyLong(),Mockito.any(DirectoryNavigations.class));
verifyNoMoreInteractions(directoryNavigationsServiceMock);
}
The test passed
When I secure DirectoryNavigationService in service layer
package phonesshop.service;
import org.springframework.security.access.annotation.Secured; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
import phonesshop.domain.DirectoryNavigations;
import java.util.List;
@EnableGlobalMethodSecurity( securedEnabled = true) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public interface DirectoryNavigationsService {
...
@Secured("ROLE_ADMIN") <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
DirectoryNavigations updateOneNavigation( long id, DirectoryNavigations oneNavigation);
....
}
and mocking user:
@Test
@WithMockUser(username="admin",roles={"USER","ADMIN"})
public void updateOneNavigation_ErrorNoEntityWithID_ReturnNoContent() throws Exception {
.....}
I have error in string
verify(directoryNavigationsServiceMock, times(1)).updateOneNavigation(Mockito.anyLong(),Mockito.any(DirectoryNavigations.class));
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.
, mergedContextConfiguration = [WebMergedContextConfiguration@433d61fb testClass = DirectoryNavigationsControllerTest, locations = '{}', classes = '{class phonesshop.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.SpringBootTestContextCustomizer@26aa12dd, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@71bbf57e, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@a688c452, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]].
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected here:
-> at phonesshop.web.DirectoryNavigationsControllerTest.updateOneNavigation_ErrorNoEntityWithID_ReturnNoContent(DirectoryNavigationsControllerTest.java:184)
-> at phonesshop.web.DirectoryNavigationsControllerTest.updateOneNavigation_ErrorNoEntityWithID_ReturnNoContent(DirectoryNavigationsControllerTest.java:184)
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.
Why an error occurs in the line, rather than earlier in this:
given(this.directoryNavigationsServiceMock.updateOneNavigation(Mockito.anyLong(),Mockito.any(DirectoryNavigations.class))
).willReturn(null);
And how can I fix this, to see if the method is called?