Java 在spring Camel Dsl路由类中使用Mockito mockbean编写junit测试会给出null
我正在使用增强的驼峰弹簧测试来测试我的驼峰路线。 我有一个类MyCamelRoutes,它包含我的骆驼路线Java 在spring Camel Dsl路由类中使用Mockito mockbean编写junit测试会给出null,java,spring-boot,mockito,apache-camel,spring-camel,Java,Spring Boot,Mockito,Apache Camel,Spring Camel,我正在使用增强的驼峰弹簧测试来测试我的驼峰路线。 我有一个类MyCamelRoutes,它包含我的骆驼路线 @Component @RefreshScope public class MyCamelRoutes extends RouteBuilder { @Autowired Environment env; @Autowired ApplicationContext appcontext; @Override public void configure() thro
@Component
@RefreshScope
public class MyCamelRoutes extends RouteBuilder {
@Autowired Environment env;
@Autowired ApplicationContext appcontext;
@Override
public void configure() throws Exception {
logger.info("env = {} and its applicationcontext = {}:end",env,appcontext);
String fileDir = env.getProperty("myfile.dir");
String noopisactive = env.getProperty("myfile.noop");
String kafkatopic = env.getProperty("mykafka.topic");
String linger = env.getProperty("mykafka.linger");
int lingerinms = Integer.parseInt(linger);
from("file://"
+ fileDir
+ "?"
+ "noop="
+ noopisactive)
.routeId("FileTaker")
.startupOrder(1)
.to("direct:processFile");
//PROCESS
from("direct:processFile")
.routeId("FileProcessor")
.startupOrder(2)
.transform()
.method(new myworker(), "process")
.to("direct:mykafka");
//KAFKA ROUTE
from("direct:mykafka")
.routeId("KafkaSender")
.startupOrder(3)
.log("Sending to zester kafka");
.to("kafka:" + kafkatopic+ "?lingerMs="+lingerinms)
.log("data sent to kafka.")
.end()
.stop();
}
}
我已经为此创建了单元测试,如下所示:
@ImportAutoConfiguration(RefreshAutoConfiguration.class)
@RunWith(CamelSpringBootRunner.class)
//@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
@DirtiesContext
@MockEndpoints("kafka:zester")
@MockBeans(@MockBean(Environment.class))
class MyCamelRoutesTest {
@EndpointInject(uri = "mock:kafka:zester")
MockEndpoint kafkamock;
@Autowired
ProducerTemplate template;
// @Autowired
// CamelContext ccontext;
//
@InjectMocks
MyCamelRoutes routes;
@Autowired
Environment env;
@BeforeTestClass
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
when(env.getProperty("myfile.dir")).thenReturn("D:/zesterFiles");
when(env.getProperty("myfile.noop")).thenReturn("true");
when(env.getProperty("mykafka.topic")).thenReturn("zester");
when(env.getProperty("mykafka.linger")).thenReturn("2000");
}
@Test
void sampleMockTest() throws InterruptedException, FileNotFoundException {
File file = ResourceUtils.getFile(
"zesterFile1.csv");
template.sendBody("file://D:/zesterFiles",file);
kafkamock.expectedMessageCount(1);
kafkamock.assertIsSatisfied();
}
}
当我试图从环境mock bean中获取值时,我得到了一个NullPointerException,即使我已经截短了正确的值。在调试模式下,我可以在Route实例中看到模拟bean,但是没有存根,因此它返回null,当解析null时,它给出NPE。
我使用的是SpringBoot2.3.0,Camel版本是3.2.0,JUnit5。谁能告诉我可能做错了什么,我如何才能让它工作