简体   繁体   English

Spring Boot应用程序中的硒-自动装配问题

[英]Selenium in spring-boot application - Autowiring issue

I am trying to run a selenium test (done through GalenFramework ) in a spring-boot application. 我正在尝试在春季启动应用程序中运行硒测试(通过GalenFramework完成)。 The issue I am facing is in the autowiring part. 我面临的问题是在自动装配部分。 I am not able to autowire the AppDetails class into my Test class because of NULLPOINTER exception. 由于NULLPOINTER异常,无法将AppDetails类自动连接到我的Test类中。

I am getting the nullpointer exception while trying to run the test alone (either directly from IntelliJ or mvn test -Dtest=TestClassName method) 尝试单独运行测试时,我遇到了nullpointer异常(直接从IntelliJmvn test -Dtest=TestClassName方法)
But I am able to run ( mvn spring-boot:run ) the application, which is not required even though I tried to run the application to make sure the value from yaml file (shirtuserurl) is reading correctly. 但是我可以运行( mvn spring-boot:run )应用程序,即使我尝试运行该应用程序以确保yaml文件(shirtuserurl)的值正确读取,也不需要这样做。

Why I used spring-boot for selenium ? 为什么我将spring-boot用作硒?
I want to load values from Application.yml file as the entire system here are set to use yaml file. 我想从Application.yml文件加载值,因为此处的整个系统都设置为使用yaml文件。

Here are the classes. 这是课程。

Application.java class Application.java类
[D:\\workspace\\galen-tester\\galen-tester\\src\\main\\java\\com\\shirt\\library\\galen\\Application.java] [D:\\ workspace \\ galen-tester \\ galen-tester \\ src \\ main \\ java \\ com \\ shirt \\ library \\ galen \\ Application.java]

package com.shirt.library.galen;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public String getTestName() {
        return "some name";
    }
}

Test Class 测试班
[D:\\workspace\\galen-tester\\galen-tester\\src\\test\\java\\com\\shirt\\library\\galen\\user\\UserTestUI.java] [D:\\ workspace \\ galen-tester \\ galen-tester \\ src \\ test \\ java \\ com \\ shirt \\ library \\ galen \\ user \\ UserTestUI.java]

package com.shirt.library.galen.user;

import com.shirt.library.galen.Application;
import com.shirt.library.galen.components.GalenTestConfig;
import com.shirt.library.galen.components.TestDevice;
import com.shirt.library.galen.models.AppDetails;
import com.galenframework.config.GalenConfig;
import com.galenframework.config.GalenProperty;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.testng.annotations.Test;

import java.io.IOException;

//@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = Application.class)
public class UserTestUI extends GalenTestConfig {

    @Autowired
    private AppDetails appDetails; <-- This comes as NULL 

    @Autowired
    String getTestName;

    @Test (dataProvider = "devices")
    public void test_user_onDevice(TestDevice devices) throws IOException {
        GalenConfig.getConfig().setProperty(GalenProperty.GALEN_RANGE_APPROXIMATION,"5");
        System.out.println("Full URL : " + appDetails.getFullURL());  <----- NULLPOINTER exception here 
        loadAppInBrowser("user");
        loginAuthWithCredentials();
        CheckLayout(devices);


    }

AppDetails class AppDetails类
[D:\\workspace\\galen-tester\\galen-commons\\src\\main\\java\\com\\shirt\\library\\galen\\models\\AppDetails.java] [D:\\ workspace \\ galen-tester \\ galen-commons \\ src \\ main \\ java \\ com \\ shirt \\ library \\ galen \\ models \\ AppDetails.java]

package com.shirt.library.galen.models;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AppDetails {

    @Value("${shirtuserurl}")
    private String fullURL;


    //@Bean
    public String getFullURL() {
        return fullURL;
    }
}

I am trying to autowire a @Component under src\\main\\java\\com\\shirt... from src\\test\\java\\com\\shirt is that the problem ? 我正在尝试从src \\ test \\ java \\ com \\ shirt下的src \\ main \\ java \\ com \\ shirt下的@Component自动接线是问题吗?

Let me know if anyone has any idea on it. 让我知道是否有人对此有任何想法。

Note: 注意:
I am using spring-boot version 1.5.10.RELEASE 我正在使用spring-boot版本1.5.10.RELEASE

try to use these annotations: 尝试使用这些注释:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration

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

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