1: When used with junit, because the configuration file is not read, the bean creation failed. I checked it online and found that I had to read the core configuration file of spring first, so that the machine can also start the IOC container.
You can first create a parent class, read the configuration file in the parent class to create an IOC container, and then let the subclass inherit it.
BaseTest.java
package com.carry.ssm.test;import javax.annotation.Resource;import javax.security.auth.PrivateCredentialPermission;import org.junit.Test;import org.junit.runner.RunWith;import com.carry.ssm.Services.TestServer;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** * Configure spring and junit integration, load springIOC container when junit starts */@RunWith(SpringJUnit4ClassRunner.class)//Tell junit spring configuration file @ContextConfiguration("classpath:aplictionContext.xml") //I put it under classpath, and I can change public class baseTest { } 2: Write test class
TestUnit.java
package com.carry.ssm.test;import javax.annotation.Resource;import org.junit.Test;import com.carry.ssm.Model.User;import com.carry.ssm.Services.TestServer;import com.carry.ssm.Services.UserService;public class TestUnit extends baseTest{@Resourceprivate TestServer bean;@Resourceprivate UserService userService; //Note that you need to use the interface here, because spring's AOP is used /*@Test*/ /* public void inteceptorTest(){ bean.ttst(); } */ @Test public void getUser(){ User user=new User(); user.setUSER_NAME("carry"); userService.login(user); } }Print as follows
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.