I'm coming onto a project that a suite of Integration tests. One of these tests autowired a class that did a lot of network calls to a 3rd party vendor. Because we didn't want our tests to make these calls, the team mocked out this client via a testApplicationContext.xml Spring configuration file.
Integration Test Class:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/testApplicationContext.xml")
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
public class IntegrationTest {
Definition of testApplication.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.mycompany" />
<context:component-scan base-package="tst.mycompany.mocks"/>
<import resource="mock-beans.xml"/>
Definition of mock-beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"...>
<!-- Override bean definitions with mocks -->
<bean id="thirdPartyClientService" class="tst.mycompany.mocks.MockThirdPartyClient" />
</beans>
This works fine in our setup and an instance of MockThirdPartyClient is autowired into our spring bean when run as a test.
However, I added a new mock for another service recently, in the same package, but cannot get it to load. I get the error:
Cannot find class [tst.mycompany.mocks.MockAdressService] for bean with name 'addressService' defined in class path resource [mock-beans.xml]; nested exception is java.lang.ClassNotFoundException: tst.mycompany.mocks.MockAdressService
Here is the updated mock-beans.xml:
<beans xmlns="http://www.springframework.org/schema/beans">
<!-- Override bean definitions with mocks -->
<bean id="thirdPartyClientService" class="tst.mycompany.mocks.MockThirdPartyClient" />
<bean id="addressService" class="tst.mycompany.mocks.MockAdressService" />
</beans>
Here's how I autowire the initial dependency that works:
@Autowired ThirdPartyClientServiceI client;
and the one that doesn't:
@Autowired AddressServiceI addressService;
I've double checked the spelling on everything and it all lines up so it's crazy that this isn't working. I thought maybe it was a case of something being cached incorrectly... but I refreshed and cleaned everything and still no dice.
Neither of these mocks has annotations FWIW, they just implement the Interface of the service they're designed to mock out. They are both located underneath the src/test/java
folder.
package tst.mycompany.mocks;
public class MockAddressService implements AddressServiceI {
the stacktrace in question:
Caused by: java.lang.ClassNotFoundException: tst.mycompany.mocks.MockAdressService
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:1.8.0_71]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_71]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[?:1.8.0_71]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_71]
at org.springframework.util.ClassUtils.forName(ClassUtils.java:250) ~[spring-core-4.2.2.RELEASE.jar:4.2.2.RELEASE]
I did check the bin folder and the compiled class is there.
It's embarrassing, but I had a typo in my bean definition in mock-beans.xml: MockAdressService should be MockAddressService (missing a 'd')