I'm triying to write a test from a controller.
The controller call a service who call an autowired repository.
Here is my class right now
#RunWith(MockitoJUnitRunner.class)
public class AdminDestinationControllerTest {
private final PTUser user = new PTUser();
private MockMvc mockMvc;
#InjectMocks
private AdminDestinationController adminDestinationController;
#Mock private RoutingDestinationServiceImpl routingDestinationService;
#Mock private PTRoutingDestinationRepository destinationRepository;
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(adminDestinationController).build();
}
#Test
public void setCost_UnknownCode() throws Exception {
when(routingDestinationService.setCost(Mockito.anyString(), Mockito.any())).thenCallRealMethod();
when(destinationRepository.setCost(Mockito.anyString(), Mockito.any())).thenReturn(Optional.empty());
mockMvc.perform(post("/admin/destinations/cost").content("{\"code\":\"99999\", \"cost\":\"2\"}").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
}
But in the service, the repository is null.
Any ideas of how I could do ?
Thank you
Related
I have 2 Test files but whenever I try to run gradle clean build,
I getting java.lang.IllegalStateException: Failed to load ApplicationContext, when i remove the #AutoConfigureMockMvc, then i get an error Could not autowire. No beans of 'MockMvc' type found.
1st File JobTest
#RunWith(SpringRunner.class)
#SpringBootTest
#AutoConfigureMockMvc
public class JobTest {
#Autowired
private WebApplicationContext applicationContext;
#Autowired
private MockMvc mockMvc;
#MockBean
private JobService jobService;
private static final String URI = "/testJob/";
#BeforeEach
void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
}
private final UUID jobId = UUID.fromString("d35089c0-8ca8-4a9d-8932-8464e9a0736c");
#Test
public void testRequestJob() throws Exception {
//create a request object
RequestBuilder requestBuilder = MockMvcRequestBuilders.post(URI)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.ALL)
.content("testRequestString");
when(jobService.neededJob(anyString()).thenReturn(mockJob);
ResultActions perform = mockMvc.perform(requestBuilder);
assertEquals(HttpStatus.OK.value(), perform.andReturn().getResponse().getStatus());
//perform the request and get the response
perform.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.data.jobId").exists());
}
}
2nd File EmployerTest
#RunWith(SpringRunner.class)
#SpringBootTest
#AutoConfigureMockMvc
public class ShiftControllerTest {
#Autowired
private WebApplicationContext applicationContext;
#Autowired
private MockMvc mockMvc;
#MockBean
private EmployerService employerService;
#BeforeEach
void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
}
private final UUID jobId = UUID.fromString("d35089c0-8ca8-4a9d-8932-8464e9a0736c");
private static final String URI = "/employer/";
#Test
public void testEmployer() throws Exception {
RequestBuilder requestBuilder = MockMvcRequestBuilders.get(URI + jobId)
.accept(MediaType.ALL);
when(employerService.getEmployer(jobId)).thenReturn(mockEmployer);
ResultActions perform = mockMvc.perform(requestBuilder);
assertEquals(HttpStatus.OK.value(), perform.andReturn().getResponse().getStatus());
}
}
If I comment one file and then try to run gradle clean build it works properly, any suggestion will be appreciated.
In the code that you have posted you don't seem to use the WebApplicationContext for anything else apart from creating a MockMvc object which you have already created anyway by virtue of the #Autowired annotation. If you don't need it for anything else try just removing the WebApplicationContext. For example:
#RunWith(SpringRunner.class)
#SpringBootTest
#AutoConfigureMockMvc
public class JobTest {
// #Autowired
// private WebApplicationContext applicationContext;
#Autowired
private MockMvc mockMvc;
#MockBean
private JobService jobService;
private static final String URI = "/testJob/";
// #BeforeEach
// void setup() {
// this.mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
// }
private final UUID jobId = UUID.fromString("d35089c0-8ca8-4a9d-8932-8464e9a0736c");
#Test
public void testRequestJob() throws Exception {
//create a request object
RequestBuilder requestBuilder = MockMvcRequestBuilders.post(URI)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.ALL)
.content("testRequestString");
when(jobService.neededJob(anyString()).thenReturn(mockJob);
ResultActions perform = mockMvc.perform(requestBuilder);
assertEquals(HttpStatus.OK.value(), perform.andReturn().getResponse().getStatus());
//perform the request and get the response
perform.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.data.jobId").exists());
}
}
Can someone point me to what could be wrong in below code. It is a boot spring 2.6.7 application. When test profile is running, it throws error for all tests like below.
java.lang.IllegalStateException: springSecurityFilterChain cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used
#AutoConfigureMockMvc
#SpringBootTest(classes = some.class)
#ActiveProfiles("test")
public class someTest {
#Autowired
private MockMvc mvc;
#Autowired
private WebApplicationContext webAppContext;
#MockBean
private SomeBean someBean;
#SpyBean
private SomeSpyBean someSpyBean;
#BeforeEach
public void setup() {
mvc = MockMvcBuilders
.webAppContextSetup(webAppContext)
.apply(springSecurity())
.build();
}
#Test
public void SomeTest1() throws Exception {
String text = "text1";
when(someBean.findStuff(text).thenReturn(Optional.of(new Thingie()));
mvc.perform(multipart("/api/somepath/")
.andExpect(status().isNotFound());
verify(someSpyBean).doStuff();
}
#Test
public void SomeTest2() throws Exception {
String text = "text2";
when(someBean.findStuff(text).thenReturn(Optional.of(new Thingie()));
mvc.perform(multipart("/api/somepath/")
.andExpect(status().isFound());
verify(someSpyBean).doStuff();
}
}
This is my integration test controller class. Method get all team and there was a problem with compilation:
#SpringJUnitWebConfig(classes = CrewApplication.class)
public class Team_Controller_Integration_Test {
private MockMvc mockMvc;
#Autowired
private WebApplicationContext webApplicationContext;
#Before
public void setup() throws Exception
{
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
MockitoAnnotations.initMocks(this);
}
#Test
void getAccount() throws Exception {
this.mockMvc.perform(get("/teams")
.accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$version").value(null))
.andExpect(jsonPath("$name").value("Apacze"))
.andExpect(jsonPath("$createOn").value(null))
.andExpect(jsonPath("modifiedOn").value(null))
.andExpect(jsonPath("$description").value("grupa programistow"))
.andExpect(jsonPath("$city").value("Włocławek"))
.andExpect(jsonPath("$headcount").value(null));
}
}
This is my error:
java.lang.NullPointerException
On the other hand I create test for Db adn I have problem becouse after add mock elements to db they return null:
#RunWith(SpringRunner.class)
#DataJpaTest
public class Team_database_integration_test {
#MockBean
private TeamRepository teamRepository;
#Autowired
private TestEntityManager testEntityManager;
#Test
public void testDb(){
Team team = new Team(1L,"teamName","teamDescription","krakow",7);
testEntityManager.persist(team);
testEntityManager.flush();
System.out.println(team);
}
}
Add this dependency declaration to test case
#Autowired
private WebApplicationContext webApplicationContext;
and correct the initialization
#Before
public void setup() throws Exception
{
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
MockitoAnnotations.initMocks(this);
}
try changing to this signature,i think content type is by default -json.Try without assertions first then add assertions for validations!
MvcResult CDTO = this.mockMvc.perform(get("/plan/1"))
.andExpect(status().isOk())
.andReturn();
I'm facing problems mocking services injected inside of other services within the Spring framework. Here is my code:
#Service("productService")
public class ProductServiceImpl implements ProductService {
#Autowired
private ClientService clientService;
public void doSomething(Long clientId) {
Client client = clientService.getById(clientId);
// do something
}
}
I want to mock the ClientService inside my test, so I tried the following:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:/spring-config.xml" })
public class ProductServiceTest {
#Autowired
private ProductService productService;
#Mock
private ClientService clientService;
#Test
public void testDoSomething() throws Exception {
when(clientService.getById(anyLong()))
.thenReturn(this.generateClient());
/* when I call this method, I want the clientService
* inside productService to be the mock that one I mocked
* in this test, but instead, it is injecting the Spring
* proxy version of clientService, not my mock.. :(
*/
productService.doSomething(new Long(1));
}
#Before
public void beforeTests() throws Exception {
MockitoAnnotations.initMocks(this);
}
private Client generateClient() {
Client client = new Client();
client.setName("Foo");
return client;
}
}
The clientService inside productService is the Spring proxy version, not the mock that I want. Is it possible to do what I want with Mockito?
You need to annotate ProductService with #InjectMocks:
#Autowired
#InjectMocks
private ProductService productService;
This will inject the ClientService mock into your ProductService.
There are more ways to achieve this, the most easy way to do this will be don't use field injection, but setter injection which means you should have:
#Autowired
public void setClientService(ClientService clientService){...}
in your service class, then you can inject your mock to the service in test class:
#Before
public void setUp() throws Exception {
productService.setClientService(mock);
}
important: If this is only a unit test, please consider don't use SpringJUnit4ClassRunner.class, but MockitoJunitRunner.class, so that you can also use field inject for your fields.
In addition to
#Autowired
#InjectMocks
private ProductService productService;
Add the following method
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
I would like to suggest you annotate the Test target with #InjectMock
Currently
#Autowired
private ProductService productService;
#Mock
private ClientService clientService;
Change to
#InjectMock
private ProductService productService;
#Mock
private ClientService clientService;
incase you still have NullPointerException for the MockingService => you can use Mockito.any() as arguments.
Hopefully, it will help you.
I am using Junit4 and Mockito for test cases, in the following code dbprop.getProperty("config") is throwing a NullPointerException because dbProp is null. Please help me out why it was not mocked?
public abstract class BaseClass {
#Autowired
protected DBproperties dbprop;
}
public class SampleClass extends BaseClass {
#Autowired
private OrderService orderService;
valdiateOrder(String input) {
String config = dbprop.getProperty("config");
}
}
public class TestSampleClass {
#InjectMocks
SampleClass sampleClass;
#Mock
private OrderService orderService;
#Test
public void testValidateOrder() {
DBproperties dbprop = mock(DBproperties .class);
when(dbprop.getProperty("config")).thenReturn("xxxx");
assertNotNull(SampleClass.valdiateOrder("xxx"));
}
}
Your dbprop mock has not been injected into sampleClass, you need to add:
#Mock
private DBproperties dbprop;
Then remove the dbprop mock creation from your test method:
#Test
public void testValidateOrder() {
// DBproperties dbprop = mock(DBproperties .class); <-- removed
when(dbprop.getProperty("config")).thenReturn("xxxx");
assertNotNull(SampleClass.valdiateOrder("xxx"));
}
Next, to ensure mocks are injected when using the #InjectMocks annotations you need to either add the following runner:
#RunWith(MockitoJUnitRunner.class)
public class TestSampleClass {
...
Or call the following in a #Before method:
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
See the MockitoAnnotations and MockitoJUnitRunner JavaDocs for more information on the two approaches.
You can annotate your Object with #Mock, so its look like this
#Mock
DBproperties dbProperties;#Before public void init(){ MockitoAnnotations.initMocks(this);
}