'When running Swing application with the Spring Framework, Unit tests do not work
I am developing a project through the spring framework. I am using JUnit to run unit tests, swing for the GUI. When I run the system, the GUI opens and I can access the database. But when I try to run the tests, I get the following errors.
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
Here is my runner class
@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 class which is contains swing components.
@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;
}
Here is my example test
@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","[email protected]","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","[email protected]","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();
}
}
How can I solve this, some parts may have been misused in the spring, I'm very new in Spring.
Solution 1:[1]
Try adding @ContextConfiguration(loader = AnnotationConfigContextLoader.class)
annotation in your TestUserService class
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | HALO |