the reason:
First, add dependency in pom.xml
Add this dependency, and note the version number to the version number of the same spring-context
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
Add junit dependency, pay attention to where it must be 4.12 or later, or be wrong
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
Second, modify the test class
1, above, was added in the annotation type, attention ContextConfiguration where to add CLASSPATH:
2, class objects automatically injected IAccountService
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
@Autowired
private IAccountService as;
@Test
public void testFindAll(){
List<Account> accounts= as.findAllAccount();
for (Account account:accounts){
System.out.println(account);
}
}
@Test
public void testFindById(){
Account account = as.findAccountById(1);
System.out.println(account);
}
@Test
public void testSaveAccount(){
Account account = new Account();
account.setName("小新");
account.setMoney(4000d);
as.saveAccount(account);
System.out.println("插入后如下:");
testFindAll();
}
@Test
public void testUpdateAccount(){
Account account = as.findAccountById(5);
account.setMoney(5000d);
as.updateAccount(account);
System.out.println("更新后如下:");
testFindAll();
}
@Test
public void testDeleteAccount(){
as.deleteAccount(5);
System.out.println("删除后如下:");
testFindAll();
}
}
Record stepped pit
Before also, add maven dependencies and found not reported red, but not actually add in, namely External Libraries under no corresponding package, right-project maven-> reimport there is no reaction, the solution is as follows:
1. Right Project -> open Module settings-> left list Libraries-> delete the wrong package (or simply delete all ctrl + a), then reimport
2, File-> SETTINGS-> Build, Execution, Deployment-> Build Tools-> Maven, check Update snapshots Always
3, see online, is not practical: imi delete files in the project, restart idea