← TestGorilla Interview Insights

TestGorilla·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
May 2026Remote

Summary

Took a Spring Framework assessment through TestGorilla for a Software Engineer role. It was a mix of multiple choice and open-ended questions covering IoC, AOP, security, and JdbcTemplate. Some of it was straightforward, a couple questions tripped me up more than I expected.

Questions Asked (8)

Q1

How do you set up a single Spring IoC container that loads from multiple config files and is shared across the whole application?

System DesignTechnical Trade-offs
Author's notes

Knew this one.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that you create a single ApplicationContext (e.g., ClassPathXmlApplicationContext or AnnotationConfigApplicationContext) and pass multiple configuration locations to its constructor. Then emphasize how to share it across the application, such as exposing it via a static holder or using Spring's ContextLoaderListener in a web app.

Pro tip: Mention that in modern Spring Boot, this is largely handled automatically, but understanding the underlying mechanism shows depth. Also, warn against creating multiple contexts, which can lead to duplicate beans and memory issues.

1. Choose the right container implementation

Decide between XML-based (ClassPathXmlApplicationContext) or annotation-based (AnnotationConfigApplicationContext) depending on your configuration style.

2. Load multiple config files

Pass an array of config locations to the container constructor, e.g., new ClassPathXmlApplicationContext("services.xml", "daos.xml").

3. Share the container application-wide

Use a static singleton holder or integrate with a web framework via ContextLoaderListener to make the context globally accessible.

4. Ensure single instantiation

Guard against multiple context creations by centralizing initialization in a bootstrap class or using Spring's built-in support.

Key Points to Mention

  • ApplicationContext vs BeanFactory
  • Constructor with multiple resource locations
  • ContextLoaderListener for web apps
  • Static singleton holder pattern
  • Avoiding multiple contexts to prevent duplicate beans
  • Spring Boot's auto-configuration as a modern alternative

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Which AOP advice type should you use if you need to log method arguments for auditing purposes? (Choose one: @Before, @After, @Around, or @AfterReturning)

Technical Trade-offsAPI & Integrations
Author's notes

Went with @Before since you want the arguments before the method runs, not after.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Evaluate each advice type based on when it executes relative to the method and what information it can access. For logging method arguments, you need access to the arguments before the method executes, so @Before is the most direct choice. However, consider if you also need to log the outcome or handle exceptions, which might require @Around.

Pro tip: Mention that @Around provides the most flexibility but comes with complexity and potential performance overhead; for simple argument logging, @Before is sufficient and cleaner. Also, note that @AfterReturning cannot access arguments unless you use a binding technique, and @After executes after the method, so arguments might be modified.

1. Understand the requirement

Identify that the goal is to log method arguments for auditing, which requires capturing the arguments at method invocation time.

2. Analyze each advice type

Consider when each advice executes and what it can access: @Before runs before method execution and can access arguments; @After runs after and can access arguments but they might be modified; @AfterReturning runs after successful return and can access return value but not arguments directly; @Around wraps the method and can access arguments and control execution.

3. Match advice to requirement

Since auditing requires logging arguments as they are passed, @Before is the simplest and most appropriate because it executes before the method and has access to the original arguments.

4. Consider trade-offs

Acknowledge that @Around could also work and provides more control (e.g., logging both arguments and return value), but it introduces complexity and potential performance overhead. For pure argument logging, @Before is sufficient.

5. Conclude with the best choice

Select @Before as the answer, explaining that it directly fulfills the requirement without unnecessary complexity.

Key Points to Mention

  • @Before advice executes before the method and can access method arguments.
  • @After advice executes after the method, but arguments might be modified by the method.
  • @AfterReturning advice executes only on successful return and cannot directly access arguments without binding.
  • @Around advice provides the most control but is more complex and can impact performance.
  • For auditing, capturing arguments at invocation time is crucial, making @Before ideal.
  • Consider if logging return values or exceptions is also needed; if so, @Around might be more suitable.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

When multiple beans implement the same interface, how do you tell Spring which specific one to inject?

Technical Trade-offsSystem Design
Author's notes

Classic qualifier question.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by acknowledging the problem: when multiple beans implement the same interface, Spring's autowiring by type fails with NoUniqueBeanDefinitionException. Then explain the main solutions—@Primary, @Qualifier, and custom qualifiers—and discuss when to use each, emphasizing trade-offs like coupling and maintainability.

Pro tip: Mention that constructor injection with @Qualifier is preferred over field injection because it makes dependencies explicit and testable, and that @Primary is best for a default implementation while @Qualifier is for specific cases.

1. Identify the ambiguity

Explain that when multiple beans of the same type exist, Spring cannot resolve by type alone and throws NoUniqueBeanDefinitionException.

2. Use @Primary for a default

Mark one bean as @Primary to indicate it should be chosen by default when no other qualifier is specified.

3. Use @Qualifier for specificity

Apply @Qualifier with the bean name at the injection point to select a specific bean, which can be combined with @Primary.

4. Consider custom qualifiers

Create custom annotations meta-annotated with @Qualifier for more semantic and type-safe selection, reducing string-based errors.

5. Discuss trade-offs and best practices

Compare approaches: @Primary is simple but can hide dependencies; @Qualifier is explicit but couples to bean names; custom qualifiers are clean but add complexity. Recommend constructor injection for testability.

Key Points to Mention

  • @Primary annotation to designate a default bean
  • @Qualifier annotation with bean name for explicit selection
  • Custom qualifier annotations for type-safe and semantic injection
  • Constructor injection vs field injection and its impact on testability
  • NoUniqueBeanDefinitionException as the error when ambiguity is unresolved
  • Bean name resolution and how it interacts with @Qualifier

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q4

What is the simplest way to inject a dependency into a Spring-managed class?

Technical Trade-offs
Author's notes

Just @Autowired on a field or constructor.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by directly answering that field injection via @Autowired is the simplest, then briefly explain why constructor injection is generally preferred for production code. Show awareness of trade-offs between simplicity and maintainability, and mention that Spring also supports setter injection and @Resource.

Pro tip: Acknowledge that while field injection is simplest to write, it hides dependencies and makes testing harder; demonstrating this nuance shows you think beyond just 'making it work'.

1. Direct Answer

State that the simplest way is field injection using @Autowired on a private field, as it requires no constructor or setter.

2. Explain How It Works

Briefly describe that Spring uses reflection to inject the dependency after bean instantiation, and that no explicit configuration is needed if component scanning is enabled.

3. Discuss Trade-offs

Mention that field injection is discouraged for production because it prevents immutability, complicates unit testing, and hides dependencies. Constructor injection is preferred for mandatory dependencies.

4. Mention Alternatives

Note that setter injection is another option for optional dependencies, and @Resource or @Inject can be used for standardization.

5. Conclude with Best Practice

Summarize that while field injection is simplest, constructor injection is the recommended approach for maintainable, testable code.

Key Points to Mention

  • @Autowired annotation for field injection
  • Constructor injection as the preferred approach
  • Setter injection for optional dependencies
  • Spring's reflection-based injection mechanism
  • Testability and immutability benefits of constructor injection
  • Component scanning and bean lifecycle

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q5

What is the easiest way to map a JDBC ResultSet to a Java object when using Spring's JdbcTemplate?

API & IntegrationsTechnical Trade-offs
Author's notes

BeanPropertyRowMapper.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying that the easiest way is to use Spring's BeanPropertyRowMapper, which automatically maps columns to bean properties by name. Then explain how to use it with JdbcTemplate.query, and briefly mention alternatives like custom RowMapper for more control. Highlight that this approach reduces boilerplate and is ideal for simple mappings.

Pro tip: Mention that BeanPropertyRowMapper requires the target class to have a default constructor and setters, and that column names must match property names (or use aliases in SQL). Also note that for performance-critical or complex mappings, a custom RowMapper might be better.

1. Identify the mapping challenge

Explain that JDBC ResultSet is low-level and manually mapping each column to object fields is tedious and error-prone.

2. Introduce BeanPropertyRowMapper

State that Spring provides BeanPropertyRowMapper, which automatically maps rows to Java beans by matching column names to property names.

3. Show usage with JdbcTemplate

Demonstrate how to pass a new instance of BeanPropertyRowMapper to JdbcTemplate.query, e.g., jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MyObject.class)).

4. Discuss alternatives and trade-offs

Mention that for complex mappings or performance, a custom RowMapper implementation might be preferable, and that BeanPropertyRowMapper uses reflection which can be slower.

Key Points to Mention

  • BeanPropertyRowMapper automatically maps columns to bean properties by name.
  • It requires the Java class to have a default constructor and setters.
  • Column names must match property names or be aliased in SQL.
  • Usage: jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MyObject.class)).
  • Alternative: custom RowMapper for complex mappings or better performance.
  • BeanPropertyRowMapper is part of Spring JDBC and reduces boilerplate code.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q6

To connect to a MySQL database in Spring, which bean type should encapsulate the database connection? (Choose one: DataSource, SQLConnector, MySqlConnector, or MySQLJDBC)

Technical Trade-offs
Author's notes

DataSource, obviously.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Identify DataSource as the correct answer, then briefly explain its role as the standard JDBC abstraction for connection management in Spring. Mention that the other options are either non-standard or specific implementations, and highlight how DataSource integrates with Spring's dependency injection and transaction management.

Pro tip: Emphasize that DataSource is an interface, allowing you to swap implementations (e.g., HikariCP, Tomcat JDBC) without changing your code, which is crucial for production flexibility. Also, note that Spring Boot auto-configures a DataSource based on the classpath and properties, simplifying setup.

1. Identify the correct bean type

State that DataSource is the standard interface for encapsulating database connections in Spring. Explain that it provides a way to obtain connections without exposing connection details.

2. Explain why other options are incorrect

Clarify that SQLConnector, MySqlConnector, and MySQLJDBC are not standard Spring bean types; they are either made-up or specific to other frameworks. This shows you understand the Spring ecosystem.

3. Describe DataSource's role in Spring

Discuss how DataSource is used by JdbcTemplate, Hibernate, and JPA for database access. Mention that it supports connection pooling and transaction management.

4. Highlight configuration and best practices

Mention that DataSource can be configured via Java config or XML, and that Spring Boot auto-configures it. Recommend using a connection pool like HikariCP for production.

5. Connect to broader trade-offs

Explain that choosing DataSource promotes loose coupling and testability, as you can easily mock or swap implementations. This aligns with the 'Technical Trade-offs' category.

Key Points to Mention

  • DataSource is the standard JDBC interface for connection management in Spring.
  • It abstracts connection details and supports connection pooling.
  • Spring Boot auto-configures a DataSource based on dependencies and properties.
  • DataSource integrates with Spring's transaction management and data access templates.
  • Other options like SQLConnector or MySqlConnector are not part of Spring's API.
  • Using DataSource allows for easy swapping of connection pool implementations (e.g., HikariCP, Tomcat JDBC).

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q7

In a Spring MVC or REST controller, how do you access the currently authenticated user's details inside a request handler for logging?

API & IntegrationsSystem Design
Author's notes

You can inject a Principal or use SecurityContextHolder to grab the Authentication object.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that Spring Security stores the authenticated principal in the SecurityContext, which can be accessed via SecurityContextHolder or injected as a method argument using @AuthenticationPrincipal. Then describe how to extract the user's details (e.g., username, ID) and log them, emphasizing thread-safety and avoiding sensitive data.

Pro tip: Mention that SecurityContextHolder uses a ThreadLocal by default, so it's safe in synchronous request handling but requires care with async or reactive flows. Also, prefer injecting @AuthenticationPrincipal over static access for testability.

1. Identify the authentication mechanism

Clarify that Spring Security is typically used, which populates the SecurityContext upon successful authentication.

2. Access the SecurityContext

Use SecurityContextHolder.getContext().getAuthentication() to retrieve the Authentication object, which contains the principal.

3. Extract user details

Cast the principal to UserDetails or your custom user class to get username, authorities, etc. Alternatively, use @AuthenticationPrincipal to inject the user directly into the controller method.

4. Log the details

Use a logger to record relevant user information (e.g., username) at an appropriate level, ensuring no sensitive data like passwords are logged.

5. Consider thread-safety and best practices

Note that SecurityContextHolder is thread-bound; for async requests, propagate the context or use other mechanisms. Prefer dependency injection for testability.

Key Points to Mention

  • SecurityContextHolder and its ThreadLocal strategy
  • Authentication object and principal
  • UserDetails interface or custom user principal
  • @AuthenticationPrincipal annotation for method argument injection
  • Logging best practices: avoid sensitive data, use appropriate log levels
  • Thread-safety considerations in asynchronous request processing

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q8

Why might @PreAuthorize("hasRole('ROLE_ABC')") cause an access denied error even for a user who should have that role, and what is the correct way to use it?

Technical Trade-offsRoot Cause Analysis
Author's notes

This one actually made me think.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining that @PreAuthorize('hasRole('ROLE_ABC')') often fails due to the automatic 'ROLE_' prefix handling in Spring Security, which can lead to double-prefixing. Then, describe the correct usage: either use hasRole('ABC') or hasAuthority('ROLE_ABC') depending on your configuration, and verify the user's granted authorities.

Pro tip: Mention that you can enable debug logging for Spring Security to see the exact authorities being checked, which quickly reveals prefix mismatches. This shows you know how to troubleshoot efficiently.

1. Identify the common pitfall

Explain that hasRole automatically adds the 'ROLE_' prefix, so using 'ROLE_ABC' results in checking for 'ROLE_ROLE_ABC', causing access denied.

2. Clarify the correct syntax

State that hasRole should be used with the role name without the prefix (e.g., hasRole('ABC')), while hasAuthority expects the full authority string (e.g., hasAuthority('ROLE_ABC')).

3. Check the user's authorities

Describe how to verify the actual authorities assigned to the user, ensuring they match the expected format (with or without 'ROLE_' prefix).

4. Consider configuration nuances

Mention that custom RolePrefix or custom PermissionEvaluator can alter behavior, so review security configuration if the issue persists.

5. Recommend debugging steps

Suggest enabling debug logging for Spring Security to see the exact access decision process and identify mismatches.

Key Points to Mention

  • Spring Security's hasRole automatically prefixes with 'ROLE_'
  • hasAuthority does not add any prefix
  • Double-prefixing leads to 'ROLE_ROLE_ABC'
  • Correct usage: hasRole('ABC') or hasAuthority('ROLE_ABC')
  • Verify user's granted authorities in the authentication object
  • Debug logging can reveal the exact authorities being checked

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.