Spring java.lang.NoClassDefFoundError:org/apache/commons/logging/Log在使用Junit模拟RestTemplate时
我正在编写一个测试用例,其中我必须模拟RestTemplate。但是当我执行测试用例时,我面临着NoClassDefFoundException。下面是我的代码Spring java.lang.NoClassDefFoundError:org/apache/commons/logging/Log在使用Junit模拟RestTemplate时,spring,junit,mockito,Spring,Junit,Mockito,我正在编写一个测试用例,其中我必须模拟RestTemplate。但是当我执行测试用例时,我面临着NoClassDefFoundException。下面是我的代码 @RunWith(MockitoJUnitRunner.class) class X{ @InjectMocks private YService yService; @Mock private RestTemplate restTemplate; @Test public void test(){ whe
@RunWith(MockitoJUnitRunner.class)
class X{
@InjectMocks
private YService yService;
@Mock
private RestTemplate restTemplate;
@Test
public void test(){
when(restTemplate.postForObject(Mockito.anyString(),Mockito.any(),Mockito.any())).thenReturn("HelloWorld");
}
}
如果我试图在build.Gradle中将commons日志依赖项作为依赖项包含,则Gradle不会下载它。您可能需要在类路径中使用apache commons日志记录
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
公用记录
公用记录
1.2
你说得对。实际上,在我的父build.gradle中排除了commons日志记录,因此,即使在我的子build.gradle中指定它,依赖关系也没有得到解决。因此,通过从我的父项目build.gradle中删除对commons日志记录的排除,事情就顺利进行了。