#Springboot test fails

1 messages · Page 1 of 1 (latest)

rustic grail
#

I'm following this course https://www.youtube.com/watch?v=Geq60OVyBPg to integrate tests into my already pretty big project. I'm getting errors all over the place, would you recommend me the best approach to integrating tests into a big project?

Software testing tutorial is a must. Specifically Unit Testing and Integration testing using Java and JUnit5 testing framework

You will learn about Junit5, Integration Tests, Assertions and Mocking with Java Mockito

#softwaretesting #softwaretestingturorial #amigoscode

Download Diffblue to auto generate unit and integration tests
► https://...

▶ Play video
grand hollowBOT
#

<@&1004656351647117403> please have a look, thanks.

grand hollowBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

rustic grail
#

this is extremely overwhelming, I can't manage to setup a decent test suite for any of my layers

#

I would literally pay for someone to link me an optimal guide on testing spring boot rest apis because this feels like black magic

#

I've tried building a functioning test suite for 3 days now I'm desperate

zealous gyro
#

we can't help you if you don't tell us what the problem is

rustic grail
#

I've wrote a proper stack overflow question

#

so It's well formatted and easy to read

#

sorry for not providing it instantly

red vault
#

@rustic grail can you provide the entire stack trace? Usually bean loading errors sort of show what dependency path caused it to be triggered

grand hollowBOT
# rustic grail

I uploaded your attachments as gist. That way, they are easier to read for everyone, especially mobile users 👍

rustic grail
hallow sun
#

@rustic grail might be because you've built a lot things till now, so obviously makes sense testing will cause lot of things to break

#

I am not sure if you are starting with unit testing first

#

You need to mock dependencies your classes are using so you won't interrupt them or cause real db connection to be established because that will eat a lot of time to execute each test

#

You need to mock and constructor-inject dependencies into your test classes

#

The error you shared above looks like a Spring MVC integration test error

rustic grail
#

I see

#

Yeah It was a misplaced bean class

#

I thought that one was an unit test honestly

#

But apparently isnt

#

I will delve into this problem

#

Can you link me any valuable resource to learn testing the proper way???

#

@hallow sun

#

Thanks for helping out

red vault
#

@rustic grail have you tried simply disabling the test profile

#

it seems the video you're using doesn't have that (or at least, the code they shared doesn't)

#

the root cause is a securityConfig bean btw, which should probably not have been loaded by the DataJpaTest annotation

red vault
#

mmh the sample code still works with the annotation, maybe it's good to compare it

grim zodiac
#

Hello!

#

What classes or modules you want to test?

#

and unit test or integration test

#

Or both?

rustic grail
#

Basically every relevant class

hallow sun
#

Sure @rustic grail I found a playlist

#

It can help you

#

here it is:

#

Learn about the different assertions that can be used with the unit testing framework: JUnit.
Understand when and how you can apply different assertions to optimise and improve the readability of your test cases.

Comment, like and subscribe for more content on Java and software engineering.

Visit my website to also see my blogs: https://anee...

▶ Play video
#

More imporatantly you will also find Stubbing which is quite indeed very useful for mimicking the behavior of your dependent classes

rustic grail
#

It seems very well done

hallow sun
#

I would say take a look at TDD also

#

That is the fundamental of producing well-tested and clean code

rustic grail
hallow sun
#

You can find a pretty much content about TDD (all talks, code walkthroughs)

#

on youtube

rustic grail
#

Will check this out (added everything in a playlist)

hallow sun
#

if you want more help, come in my DM, I will

rustic grail
#

I will leave this thread open while learning

#

Maybe will post some updates

grand hollowBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍

rustic grail
#

hey, so for lack of time & will I ended up making half coverage. I solved the HandlerExceptionResolver error getting a good service/repository coverage with @DataJpaTest
but when moving on to integration testing the controller it complains giving me all those errors...

this is now the erro I'm getting: Error creating bean with name 'applicationLoggingAspect' defined in file [...] Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationService' defined in file

This is how I've set up my testing suite:

@WebMvcTest(UserController.class)
@ActiveProfiles("test")
public class UserControllerTest {
    @MockBean
    private UserService userService;
    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private RoleService roleService;

    @Test
    void auth() throws Exception {

        UserDto dto= createBasicDto();

        when(userService.getUserDtoByKey(eq(dto.getKey()))).thenReturn(dto);
        mockMvc.perform(get("/users/auth/")
                        .header("key",dto.getKey()))
                .andExpect(status().isAccepted());
    }
}

applicationLoggingAspect is an aspect class that just takes up a service and its dto. I use it to log stuff inside my api...

@Aspect
@Component
@Slf4j
public class ApplicationLoggingAspect extends BaseLoggingAspect<ApplicationService, ApplicationDto> {

    public ApplicationLoggingAspect(ApplicationService service) {
        super(service);
    }

    @Autowired
    LogService logService;
[...]
}```
grand hollowBOT
# rustic grail hey, so for lack of time & will I ended up making half coverage. I solved the Ha...

Detected code, here are some useful tools:

Formatted code
@WebMvcTest(UserController.class ) @ActiveProfiles("test") public class UserControllerTest {
  @MockBean
  private UserService userService;
  @Autowired
  private MockMvc mockMvc;
  @MockBean
  private RoleService roleService;
  @Test
  void auth() throws Exception {
    UserDto dto = createBasicDto();
    when(userService.getUserDtoByKey(eq(dto.getKey()))).thenReturn(dto);
    mockMvc.perform(get("/users/auth/").header("key", dto.getKey())).andExpect(status().isAccepted());
  }
}
rustic grail
#

it was an dependency injection problem, I solved! :)) really thanks to @hallow sun

grand hollowBOT
#

Closed the thread.