vuvgaG
vuvgaG

Reputation: 15

Can't run a Spring Boot test for a simple controller

I've got a simple test:

package com.example.app;

import com.example.app.HomeController;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class HomeControllerTest {

    @Autowired
    private HomeController controller;
    
}

and a Controller HomeController

The test lives in test/java/com/example/app and the controller lives in java/com/example/app

Why can the import not be resolved?

It don't work without it either, which it what the tutorial suggests.

Without the import I still get the image of static typing error

Edit:

.
├── [  96]  .mvn
│   └── [ 160]  wrapper
├── [ 128]  src
│   ├── [ 128]  main
│   └── [  96]  test
├── [1.9K]  .classpath
├── [ 395]  .gitignore
├── [ 835]  .project
├── [1.6K]  HELP.md
├── [9.8K]  mvnw
├── [6.5K]  mvnw.cmd
└── [1.6K]  pom.xml

I created the package from Visual Studio Code, but its just a simple maven init template.

Upvotes: 0

Views: 1133

Answers (2)

mentallurg
mentallurg

Reputation: 5207

Your test class contains no test methods. That's why nothing happens when you try to execute it.

What can you do? Implement at least one test method and annotate it with @Test.

Update:

The reason was a look up of a wrong name. No magical reasons. Everyone makes mistakes.

Upvotes: 1

Anuja Paradkar
Anuja Paradkar

Reputation: 187

This is my example, I tried running and it worked -

@SpringBootTest
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
public class MainApplicationTest {
  
  @Autowired
  HomeController homeController;
  @Autowired
  OtherController otherController;

  @Test
  public void testContextLoading() throws Exception{
    assertThat(homeController).isNotNull();
    assertThat(otherController).isNotNull();
  }

}

Upvotes: 1

Related Questions