#unit testing error

1 messages · Page 1 of 1 (latest)

lost sierra
#

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"));
}
}
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.```

white ermineBOT
#

This post has been reserved for your question.

Hey @lost sierra! 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.

white ermineBOT
#

💤 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.

mental arch
#

try to annotate it with springboottest instead of what u have

white ermineBOT
#

💤 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.

lost sierra
# mental arch try to annotate it with springboottest instead of what u have

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.```

mental arch
#

show the code

#

i mean the test

lost sierra
#

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"));
    }
}
mental arch
#

if you wanna use mockmvc

#

you also need that annotation

#
@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

lost sierra
#

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"));
}
}

#

u mean something like this?

mental arch
#

get rid of customer service

lost sierra
#

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.```

mental arch
lost sierra
#

Ik

mental arch
#

show the

#

class again

#

after you deleted the service in the test

lost sierra
#
@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"));
    }
}
mental arch
#

also make sure src/main/java and src/test/java have the same packages

lost sierra
#

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

mental arch
#

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

lost sierra
mental arch
#

hmmmmmmmmmmmmm

mental arch
#

MaxLibrary

#

as for the test

#

can you check if

#

max library application test works

#

or if you get the same error

lost sierra
#

alright I renamed it

#

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.```

mental arch
#

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

lost sierra
#

ok I figured it out

#

@SpringBootTest(classes = MaxLibraryApplication.class)

#

just had to add the "classes = "

mental arch
#

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

lost sierra
#

ohhh shit

#

so you generally have to put all the packages into the com.blahblah package

mental arch
#

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

lost sierra
#

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

mental arch
#

yea i guess

#

look

#

i took it from some example

#

ignore the serverlet initializer

#

thats how it looks

white ermineBOT
# lost sierra well thanks for the help dude

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.