当使用Spring Framework运行Swing应用程序时,单元测试不起作用



我正在通过spring框架开发一个项目。我正在使用JUnit来运行单元测试,为GUI摆动。当我运行系统时,GUI打开,我可以访问数据库。但是,当我尝试运行测试时,会出现以下错误。

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cs320MtsApplication': Unsatisfied dependency expressed through field 'application'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'application' defined in file

这是我的跑步类

@SpringBootApplication
public class Cs320MtsApplication implements CommandLineRunner {
@Autowired
Application application;
public static void main(String[] args) {
new SpringApplicationBuilder(Cs320MtsApplication.class).headless(false).run(args);
}
@Override
public void run(String... args) {
application.init();
}
}

Application类,该类包含回转组件。

@Component
public class Application extends JFrame {
@Autowired
private TransferOthersAccount   transferOthers;
@Autowired
private Register                register;
@Autowired
private CheckBalance            checkBalance;
@Autowired
private MainMenu                mainMenu;
@Autowired
private ViewTransactionHist     viewTransaction;
@Autowired
private Welcome                 welcome;
@Autowired
private ChangePassword          changePassword;
@Autowired
private TransferOwnAccount      transferOwn;
@Autowired
private Login                   login;
}

这是我的示例测试

@SpringBootTest
public class TestUserService
{
@Autowired
UserService userService;
@Autowired
AccountService accountService;
/**
* The default DB mode is "create"
* This test creates 2 user and 2 account for each user.
* This method should be run in order to initialize the DB and work with it for testing purposes.
*/
@Test
public void testCreateUser() throws Exception {
// Creation of first User
User user = new User("Kerem","Ersan",123456,"07-04-2000","35007269396","kerem.ersan@ozu.edu.tr","5057656825");
// Creation of first User's first account.
Account account = new Account(300,user);
user.getAccounts().add(account);
account.setUser(user);
// Creation of first User's second account.
Account account2 = new Account(200,user);
user.getAccounts().add(account2);
account.setUser(user);
// saves first User information to the DB
userService.save(user);
// Creation of second User
User user2 = new User("Zuhal","Karabas",973100,"05-06-1998","24107854293","zuhaltokgoz@gmail.com.tr","5511768127");
// Creation of second User's first account
Account account3 = new Account(1575,user2);
user2.getAccounts().add(account3);
account.setUser(user2);
// Creation of second User's second account
Account account4 = new Account(1870,user2);
user2.getAccounts().add(account4);
account4.setUser(user2);
// saves second User information to the DB
userService.save(user2);

}
@Test
public void deleteAllRows()
{
userService.deleteAll();
}
}

我该如何解决这个问题,有些零件可能在春天被误用了,我在春天很新。

尝试在TestUserService类中添加@ContextConfiguration(loader = AnnotationConfigContextLoader.class)注释

最新更新