gpt4 book ai didi

java - @ComponentScan 无法使用 spring-boot-starter-test 进行测试

转载 作者:行者123 更新时间:2023-11-30 08:23:25 31 4
gpt4 key购买 nike

我正在尝试使用 spring-boot-starter-test 和 @Autowired 在我的项目中测试我的 @Service@Repository 类不适用于我正在测试的类(class)。

单元测试:

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = HelloWorldConfiguration.class
//@SpringApplicationConfiguration(classes = HelloWorldRs.class)
//@ComponentScan(basePackages = {"com.me.sbworkshop", "com.me.sbworkshop.service"})
//@ConfigurationProperties("helloworld")
//@EnableAutoConfiguration
//@ActiveProfiles("test")
// THIS CLASS IS IN src/test/java/ AND BUILDS INTO target/test-classes
public class HelloWorldTest {

@Autowired
HelloWorldMessageService helloWorldMessageService;

public static final String EXPECTED = "je pense donc je suis-TESTING123";

@Test
public void testGetMessage() {
String result = helloWorldMessageService.getMessage();
Assert.assertEquals(EXPECTED, result);
}
}

服务:

@Service
@ConfigurationProperties("helloworld")
// THIS CLASS IS IN /src/main/java AND BUILDS INTO target/classes
public class HelloWorldMessageService {

private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message=message;
}

}

单元测试上的注释类注释代表了我为使它正常工作而尝试的各种事情。测试包和项目包位于相同的包路径中,@ComponentScan 从我的入口点(@RestController 类和 main 方法)工作正常。服务 @ComponentScan@Autowire 在我的 src/main/java 端的 @RestController 类中很好,但不是在测试中。我需要将它作为 @Bean 再次添加到我的 @Configuration 类中,以便 @Autowired 工作。该类在其他方面都很好,我可以从测试中很好地引用和实例化它。问题似乎是 @ComponentScan 似乎没有正确遍历我的测试运行器类路径中的多个条目,在本例中为/target/test-classes 和/target/classes。

我使用的IDE是IntelliJ IDEA 13。

更新 - 这是 HelloWorldRs 及其配置:

@RestController
@EnableAutoConfiguration
@ComponentScan
public class HelloWorldRs {

// SPRING BOOT ENTRY POINT - main() method
public static void main(String[] args) {
SpringApplication.run(HelloWorldRs.class);
}

@Autowired
HelloWorldMessageService helloWorldMessageService;

@RequestMapping("/helloWorld")
public String helloWorld() {
return helloWorldMessageService.getMessage();
}

}

...

@Configuration
public class HelloWorldConfiguration {

@Bean
public Map<String, String> map() {
return new HashMap<>();
}

// This bean was manually added as a workaround to the @ComponentScan problem
@Bean
public HelloWorldMessageService helloWorldMessageService() {
return new HelloWorldMessageService();
}

// This bean was manually added as a workaround to the @ComponentScan problem
@Bean
public HelloWorldRs helloWorldRs() {
return new HelloWorldRs();
}
}

最佳答案

首先,我建议使用更新的 @RunWith(SpringRunner.class) 但这没有区别,它只是更短(推荐)。

其次,从 @EnableAutoConfiguration 我看到您正在使用 spring boot - 这当然是一件好事。 直接使用@ComponentScan 有一些很好的理由。您可以尝试以下操作吗?

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes=YourApplication_or_other_Configuration.class)
public class HelloWorldTest {
... etc.

关于java - @ComponentScan 无法使用 spring-boot-starter-test 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23701616/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com