Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.example.inventorymanagement.entity.Users;
import com.example.inventorymanagement.repository.UserRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.security.crypto.password.PasswordEncoder;

@SpringBootApplication
Expand All @@ -19,11 +19,22 @@ public static void main(String[] args) {
}

@Bean
@ConditionalOnBean(UserRepository.class)
public CommandLineRunner initAdmin(UserRepository userRepository,
PasswordEncoder passwordEncoder,
public CommandLineRunner initAdmin(ObjectProvider<UserRepository> userRepositoryProvider,
ObjectProvider<PasswordEncoder> passwordEncoderProvider,
Environment environment) {
return args -> {
UserRepository userRepository = userRepositoryProvider.getIfAvailable();
if (userRepository == null) {
// In sliced tests (e.g., @WebMvcTest), repository beans are not loaded.
return;
}

PasswordEncoder passwordEncoder = passwordEncoderProvider.getIfAvailable();
if (passwordEncoder == null) {
// In some test slices, security beans are not loaded.
return;
}
Comment thread
TheWorthless11 marked this conversation as resolved.
Comment thread
TheWorthless11 marked this conversation as resolved.

String adminUsernameEnv = environment.getProperty("ADMIN_USERNAME");
String adminUsername = (adminUsernameEnv == null || adminUsernameEnv.isBlank())
? "admin"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.core.env.Environment;
import org.springframework.security.crypto.password.PasswordEncoder;

Expand Down Expand Up @@ -88,10 +89,10 @@ void missingOrBlankPasswordSkipsSafely() {
@Configuration
static class RunnerConfig {
@Bean
CommandLineRunner testCommandLineRunner(UserRepository userRepository,
PasswordEncoder passwordEncoder,
CommandLineRunner testCommandLineRunner(ObjectProvider<UserRepository> userRepositoryProvider,
ObjectProvider<PasswordEncoder> passwordEncoderProvider,
Environment environment) {
return new InventoryManagementApplication().initAdmin(userRepository, passwordEncoder, environment);
return new InventoryManagementApplication().initAdmin(userRepositoryProvider, passwordEncoderProvider, environment);
}
}
}
Expand Down
Loading