问题描述
我正在使用增强的骆驼弹簧测试来测试我的骆驼路线。 我有一个MyCamelRoutes类,其中包含我的骆驼路线。
@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();
}
}
即使我已经存入了正确的值,但尝试从环境模拟bean中获取值时却遇到了NullPointerException异常。在调试模式下,我可以在Route实例中看到模拟的bean,但是那里没有存根,因此它返回null,并且在解析null时会给出NPE。 我正在使用Spring Boot 2.3.0,Camel版本是3.2.0,junit 5。谁能告诉我我可能在做错什么,如何使它起作用?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)