Why .isNotNull(); assertions fails even though I have a controller class?

328 Views Asked by At

I am new to Spring Boot and testing. I am currently studying from baeldung's tutorial (https://spring.io/guides/gs/testing-web/) and I bumped into this error.

enter image description here

In fact, it passes when I use isNull() method. But I don't understand this because my controller class has mappings and all that. Why would it be null?

This is my controller class

enter image description here

Or maybe this is not how this test is supposed to be done? But they show it as follows in baeldung

enter image description here

Any help is appreciated.

Best,

2

There are 2 best solutions below

7
hiren On BEST ANSWER

If you want to run the testcases with spring context you need to provide @SpringBootTest annotation at your test class (i.e instead of @SpringBootConfiguration at your test class Smoke). The other option apart from @SpringBootTest are as below.

for junit4:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class YourTestClass {}

for junit 5:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class YourTestClass

Where SpringTestConfiguration class hold's all the beans you need to autowire. For more details of SpringRunner vs SpringBootTest read the article

0
pens On

Have you tried assertNotNull(studentController)?