简体   繁体   中英

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静态输入错误的图像

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.

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.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM