繁体   English   中英

Spring Boot 2.1测试由于空属性而失败

[英]Spring boot 2.1 test fails because of null property

我不是Spring Boot的专家。 我必须为@RestController方法编写测试,但是有一个问题,即当测试类执行main controller时, @AutoWired ConfigurationProperties类为null 我在这里找到了很多有关类似问题的帖子,但实际上并不能解决这个问题。 奇怪的是,在@RestController的@PostConstruct方法中,属性类不为null,仅在我要测试的@RequestMapping方法中为null。

这是我的@SpringBootApplication类:

@SpringBootApplication
@ComponentScan
@EnableConfigurationProperties({MysqlProperties.class, CassandraProperties.class, GenericsProperties.class})
@EnableAutoConfiguration
public class REST {
    public static void main(String[] args) {
        SpringApplication.run(REST.class, args);
    }
}

这是@RestController:

@RestController
public class MainController {

    @Autowired
    private MysqlProperties mysqlProperties;

    @PostConstruct
    public void init() throws Exception {
       //Here mysqlProperties is not null and I can get elements from it
    }

    @RequestMapping(value = "/online", method = RequestMethod.GET)
    public @ResponseBody
    String online(@RequestHeader(value = "email", required = true) String email, @RequestHeader(value = "password", required = true) String password) {
    Utils.logInfo(logger, "/online endpoint");

    //Here mysqlProperties is null
    String sql = "SELECT * FROM " + mysqlProperties.getAddress() + " WHERE email= ?";

    return new Return(Return.ERROR_MESSAGE, "Access denied, not superuser").toString();
}

这是@ConfigurationProperties类:

@Configuration
@PropertySource("classpath:application.properties")
@ConfigurationProperties(prefix = "mysql")
public class MysqlProperties {

    String address;
    String database;
    ...

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }
}

这是测试:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {REST.class})
@EnableConfigurationProperties({CassandraProperties.class, GenericsProperties.class, MysqlProperties.class})
@AutoConfigureMockMvc
public class MainControllerTest {

    @Autowired
    private MockMvc mvc;

    @Autowired
    private GenericsProperties genericsProperties;

    @Before
    public void init() {
        try {
            //mc.init();
            mvc = MockMvcBuilders.standaloneSetup(new MainController()).build();
        } catch (Exception ex) {
            ex.printStackTrace();
            Logger.getLogger(MainControllerTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Test
    public void testOnline() throws Exception {
        //Return returnObject = new Return(Return.DONE_MESSAGE, "System online");
        Return returnObject = new Return(Return.ERROR_MESSAGE, "Access denied, not superuser");

        this.mvc.perform(get("/online")
                .header("email", genericsProperties.getSuperuser_email())
                .header("password", genericsProperties.getSuperuser_password()))
                //.contentType(APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().json(returnObject.toString()));
    }
}

这是包的结构:

main
--java
----configurations
------MysqlProperties.java
----main
------MainController.java
----...
--resources
----application.properties
test
--java
----main
------MainControllerTest.java

NullPointerException发生在MainController类中,位于:

mysqlProperties.getAddress()

任何线索为什么它不起作用? 谢谢。

@Autowired MysqlProperties mysqlProperties为空,因为您在此处创建了MainController的新实例:
mvc = MockMvcBuilders.standaloneSetup(new MainController()).build();
该实例将不会在Spring上下文中注册,因此将无法用于依赖项注入。
您可以在此处阅读有关此问题的更多信息

您应该在MainControllerTest类中使用自动装配的MainController

@Autowired
private MainController mainController;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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