unit testing error

I wrote this unit test:
@WebMvcTest(CustomerController.class)
public class CustomerControllerTest {
@Autowired
private MockMvc mockMvc;

@Autowired
private CustomerService customerService;

@Test
void registerUser_validInput_returnsSuccess() throws Exception {
CustomerDto customerDto = new CustomerDto("m", "k", "[email protected]", "password", "borrower");

Mockito.doNothing().when(customerService).createCustomer(any(CustomerDto.class));

mockMvc.perform(post("/customer/register")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"firstName\":\"m\",\"lastName\":\"k\",\"email\":\"email
@gmail.com\",\"password\":\"password\",\"role\":\"borrower\"}"))
.andExpect(status().isOk())
.andExpect(content().string("Added a new customer"));
}
}
@WebMvcTest(CustomerController.class)
public class CustomerControllerTest {
@Autowired
private MockMvc mockMvc;

@Autowired
private CustomerService customerService;

@Test
void registerUser_validInput_returnsSuccess() throws Exception {
CustomerDto customerDto = new CustomerDto("m", "k", "[email protected]", "password", "borrower");

Mockito.doNothing().when(customerService).createCustomer(any(CustomerDto.class));

mockMvc.perform(post("/customer/register")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"firstName\":\"m\",\"lastName\":\"k\",\"email\":\"email
@gmail.com\",\"password\":\"password\",\"role\":\"borrower\"}"))
.andExpect(status().isOk())
.andExpect(content().string("Added a new customer"));
}
}
but I'm getting this error:
Test ignored.

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.
Test ignored.

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.
38 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @IamMax420! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
ayylmao123xdd
ayylmao123xdd2mo ago
try to annotate it with springboottest instead of what u have
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
IamMax420
IamMax420OP2mo ago
now I get this:
Test ignored.

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.
Test ignored.

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.
ayylmao123xdd
ayylmao123xdd2mo ago
show the code i mean the test
IamMax420
IamMax420OP2mo ago
package controllers;

import com.max420.MaxLibrary.MaxLibraryApplication;
import dto.CustomerDto;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.mockito.ArgumentMatchers.any;
import services.CustomerService;

@SpringBootTest
public class CustomerControllerTest {
@Autowired
private MockMvc mockMvc;

@Autowired
private CustomerService customerService;

@Test
void registerUser_validInput_returnsSuccess() throws Exception {
CustomerDto customerDto = new CustomerDto("m", "k", "[email protected]", "password", "borrower");

Mockito.doNothing().when(customerService).createCustomer(any(CustomerDto.class));

mockMvc.perform(post("/customer/register")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"firstName\":\"m\",\"lastName\":\"k\",\"email\":\"[email protected]\",\"password\":\"password\",\"role\":\"borrower\"}"))
.andExpect(status().isOk())
.andExpect(content().string("Added a new customer"));
}
}
package controllers;

import com.max420.MaxLibrary.MaxLibraryApplication;
import dto.CustomerDto;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.mockito.ArgumentMatchers.any;
import services.CustomerService;

@SpringBootTest
public class CustomerControllerTest {
@Autowired
private MockMvc mockMvc;

@Autowired
private CustomerService customerService;

@Test
void registerUser_validInput_returnsSuccess() throws Exception {
CustomerDto customerDto = new CustomerDto("m", "k", "[email protected]", "password", "borrower");

Mockito.doNothing().when(customerService).createCustomer(any(CustomerDto.class));

mockMvc.perform(post("/customer/register")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"firstName\":\"m\",\"lastName\":\"k\",\"email\":\"[email protected]\",\"password\":\"password\",\"role\":\"borrower\"}"))
.andExpect(status().isOk())
.andExpect(content().string("Added a new customer"));
}
}
ayylmao123xdd
ayylmao123xdd2mo ago
if you wanna use mockmvc you also need that annotation
@AutoConfigureMockMvc
@AutoConfigureMockMvc
also if you wanna test the controller you shouldnt use mockito on customer service because you wanna test the whole flow controller service database so you load an actual object so you dont really need customer service at all here
IamMax420
IamMax420OP2mo ago
not sure if I understand right:
@SpringBootTest
@AutoConfigureMockMvc
public class CustomerControllerTest {
@Autowired
private MockMvc mockMvc;

@Autowired
private CustomerService customerService;

@Test
void registerUser_validInput_returnsSuccess() throws Exception {
CustomerDto customerDto = new CustomerDto("m", "k", "@gmail.com", "password", "borrower");

mockMvc.perform(post("/customer/register")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"firstName\":\"m\",\"lastName\":\"k\",\"email\":\"email
@gmail.com\",\"password\":\"password\",\"role\":\"borrower\"}"))
.andExpect(status().isOk())
.andExpect(content().string("Added a new customer"));
}
}
@SpringBootTest
@AutoConfigureMockMvc
public class CustomerControllerTest {
@Autowired
private MockMvc mockMvc;

@Autowired
private CustomerService customerService;

@Test
void registerUser_validInput_returnsSuccess() throws Exception {
CustomerDto customerDto = new CustomerDto("m", "k", "@gmail.com", "password", "borrower");

mockMvc.perform(post("/customer/register")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"firstName\":\"m\",\"lastName\":\"k\",\"email\":\"email
@gmail.com\",\"password\":\"password\",\"role\":\"borrower\"}"))
.andExpect(status().isOk())
.andExpect(content().string("Added a new customer"));
}
}
u mean something like this?
ayylmao123xdd
ayylmao123xdd2mo ago
get rid of customer service
IamMax420
IamMax420OP2mo ago
now I'm getting this:
Test ignored.

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.
Test ignored.

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.
ayylmao123xdd
ayylmao123xdd2mo ago
also you probably shouldnt put your real name in tests
IamMax420
IamMax420OP2mo ago
Ik
ayylmao123xdd
ayylmao123xdd2mo ago
show the class again after you deleted the service in the test
IamMax420
IamMax420OP2mo ago
@SpringBootTest
@AutoConfigureMockMvc
public class CustomerControllerTest {
@Autowired
private MockMvc mockMvc;

@Test
void registerUser_validInput_returnsSuccess() throws Exception {
CustomerDto customerDto = new CustomerDto("m", "k", "[email protected]", "password", "borrower");

mockMvc.perform(post("/customer/register")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"firstName\":\"m\",\"lastName\":\"k\",\"email\":\"[email protected]\",\"password\":\"password\",\"role\":\"borrower\"}"))
.andExpect(status().isOk())
.andExpect(content().string("Added a new customer"));
}
}
@SpringBootTest
@AutoConfigureMockMvc
public class CustomerControllerTest {
@Autowired
private MockMvc mockMvc;

@Test
void registerUser_validInput_returnsSuccess() throws Exception {
CustomerDto customerDto = new CustomerDto("m", "k", "[email protected]", "password", "borrower");

mockMvc.perform(post("/customer/register")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"firstName\":\"m\",\"lastName\":\"k\",\"email\":\"[email protected]\",\"password\":\"password\",\"role\":\"borrower\"}"))
.andExpect(status().isOk())
.andExpect(content().string("Added a new customer"));
}
}
ayylmao123xdd
ayylmao123xdd2mo ago
also make sure src/main/java and src/test/java have the same packages
IamMax420
IamMax420OP2mo ago
so If I have a package named "config" in the /main dir, I have to create it in the /test dir even if I'm not planning on testing it now? or what
ayylmao123xdd
ayylmao123xdd2mo ago
no show the screenshot of the classes if you can if you cant make sure the package customer controller is in main java is the same as in test java
IamMax420
IamMax420OP2mo ago
No description
IamMax420
IamMax420OP2mo ago
it is there
ayylmao123xdd
ayylmao123xdd2mo ago
hmmmmmmmmmmmmm package name shouldnt be uppercase btw MaxLibrary as for the test can you check if max library application test works or if you get the same error
IamMax420
IamMax420OP2mo ago
alright I renamed it
No description
IamMax420
IamMax420OP2mo ago
still getting an error tho:
Test ignored.

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.
Test ignored.

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.
ayylmao123xdd
ayylmao123xdd2mo ago
oh lol ok so you see that package named com.max420.maxlibrary in src java place all the folders in it so config controllers dto models repository services it should be in that package and in your test java package move the controllers folder to com.max420.maxlibrary then send a screenshot of how it looks
IamMax420
IamMax420OP2mo ago
ok I figured it out
@SpringBootTest(classes = MaxLibraryApplication.class)
@SpringBootTest(classes = MaxLibraryApplication.class)
just had to add the "classes = "
ayylmao123xdd
ayylmao123xdd2mo ago
yea but you could do it without specifying the class because you have a quite interesting project structure if you did my suggestion u wouldnt need to specify the class u can try it out if ur curious
IamMax420
IamMax420OP2mo ago
ohhh shit so you generally have to put all the packages into the com.blahblah package
ayylmao123xdd
ayylmao123xdd2mo ago
well yes r u learning from a tutorial or a course btw if its a course u should ask the instructor how project structure should look like basically like this yea
IamMax420
IamMax420OP2mo ago
I watched some course from freeCodeCamp some time ago and the guy was creating a bunch of separate packages but apparently thats not what you're supposed to do
ayylmao123xdd
ayylmao123xdd2mo ago
yea i guess
ayylmao123xdd
ayylmao123xdd2mo ago
No description
ayylmao123xdd
ayylmao123xdd2mo ago
look i took it from some example ignore the serverlet initializer thats how it looks
IamMax420
IamMax420OP2mo ago
well thanks for the help dude
JavaBot
JavaBot2mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot2mo ago
Post Closed
This post has been closed by <@589517424093691905>.

Did you find this page helpful?