Java 为什么Windows和Linux机器处理Spring应用程序集成测试的方式不同?
(请原谅我的措辞选择——我对Java/Spring/PowerMock/SoapUI缺乏经验) 我做了一个导致数据库调用的代码更改,现在我在Spring应用程序的集成测试中失败了。结构与此类似:Java 为什么Windows和Linux机器处理Spring应用程序集成测试的方式不同?,java,spring,maven,integration-testing,Java,Spring,Maven,Integration Testing,(请原谅我的措辞选择——我对Java/Spring/PowerMock/SoapUI缺乏经验) 我做了一个导致数据库调用的代码更改,现在我在Spring应用程序的集成测试中失败了。结构与此类似: // Integration tests class @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = MyConfigurationClass.class) @WebIntegration
// Integration tests class
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyConfigurationClass.class)
@WebIntegrationTest
@PrepareForTest({..., MyDbConnector.class, ...}) // MyDbConnector is a wrapper around JdbcTemplate
...
@Rule
public PowerMockRule powerMockRule = new PowerMockRule();
@Rule
public ExpectedException expectedException = ExpectedException.none();
...
@Before
public void setUp() {
...
whenNew(MyDbConnector.class).withNoArguments().thenReturn(mock(MyDbConnector.class));
...
}
// MyConfigurationClass.java
@SpringBootApplication
@ComponentScan(basePackages = ...,
excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = someClass.class),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = someOtherClass.class)
}
)
public final class MyConfigurationClass {}
在我更改之前,该构建在Linux和Windows上成功运行。
更改后,构建在Windows上成功运行,但在Linux上失败。Linux上的构建似乎试图通过调用MyDbConnector
中的方法连接到数据库,但无法执行SQL查询。因为MyDbConnector
应该被模拟出来,所以它甚至不应该进入真正的方法
为什么Windows maven构建(似乎)成功地模拟了MyDbConnector
,而Linux maven构建没有成功?我已经确认了两个操作系统上的测试运行
潜在重要注意事项:
- 我有两个集成测试类,一个只对Spring进行测试,另一个使用
对Spring运行SoapUI测试WsdlProjectRunner
- 我在两台不同的Linux机器上尝试了构建。在一台机器上,只有SoapUI测试失败,但在另一台机器上,SoapUI和非SoapUI测试失败
@PrepareForTest({..., MyDbConnector.class, ...})
我补充说:
@PrepareForTest({..., MyDbConnector.class, ClassThatUsesMyDbConnector.class, ...})
这解决了我们的问题,但没有回答问题,因此我将保留它。您使用的是powermock规则吗?您是否有一个
mockStatic(MyDbConnector.class)
,安装方法的完整代码可能有助于获得答案否我没有。当我尝试在whenNew()
之前添加mockStatic(MyDbConnector.class)
时,我得到了一个null
指针异常。我将重试以获取完整错误。Powermock需要一个特定的运行程序PowerMockRunner
或junit@Rule
,您应该阅读文档。我看不到全部情况,但我建议在测试中使用“mock”(仅此而已)或spring(例如内存中的DB)@RC。对不起,我走的时候想把它捡起来。我添加了缺少的@Rule
s注释。这些是你指的文件吗?是的,wiki包含文档:您使用的是powermock规则吗?您是否有一个mockStatic(MyDbConnector.class)
,安装方法的完整代码可能有助于获得答案否我没有。当我尝试在whenNew()
之前添加mockStatic(MyDbConnector.class)
时,我得到了一个null
指针异常。我将重试以获取完整错误。Powermock需要一个特定的运行程序PowerMockRunner
或junit@Rule
,您应该阅读文档。我看不到全部情况,但我建议在测试中使用“mock”(仅此而已)或spring(例如内存中的DB)@RC。对不起,我走的时候想把它捡起来。我添加了缺少的@Rule
s注释。这些是你指的文件吗?是的,wiki包含以下文档: