Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitos35 committed Dec 1, 2024
1 parent 376267c commit 3250ed4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
// JUnit Jupiter
testImplementation platform("org.junit:junit-bom:${versions.junit}")
testImplementation "org.junit.jupiter:junit-jupiter"
testImplementation "org.mockito:mockito-core:${versions.mockito}"
}

checkstyle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public class BeanDefinitionRegistryImpl implements BeanDefinitionRegistry {
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
if (beanDefinitionMap.containsKey(beanName)) {
throw new BeanDefinitionStoreException("Cannot register bean definition with name '" + beanName +
"': another bean with the same name already exists and overriding is not allowed.");
throw new BeanDefinitionStoreException(String.format("Cannot register bean definition with name '%s' " +
"another bean with the same name already exists and overriding is not allowed.", beanName));
}
beanDefinitionMap.put(beanName, beanDefinition);
}

@Override
public void removeBeanDefinition(String beanName) {
if (!beanDefinitionMap.containsKey(beanName)) {
throw new BeanDefinitionStoreException("No bean definition found for name '" + beanName + "'");
throw new BeanDefinitionStoreException( String.format("No bean definition found for name '%s'", beanName));
}
beanDefinitionMap.remove(beanName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.codeus.winter.context;

import com.codeus.winter.config.BeanDefinition;
import com.codeus.winter.config.BeanDefinitionRegistry;
import com.codeus.winter.config.BeanFactory;
import com.codeus.winter.config.BeanPostProcessor;
import com.codeus.winter.config.ClassPathBeanDefinitionScanner;
import com.codeus.winter.config.DefaultBeanFactory;
import com.codeus.winter.config.impl.BeanDefinitionRegistryImpl;
import com.codeus.winter.exception.BeanNotFoundException;
import jakarta.annotation.Nullable;
import org.apache.commons.lang3.ObjectUtils;
Expand All @@ -24,15 +26,17 @@ public class AnnotationApplicationContext implements ApplicationContext, BeanFac
private String displayName = ObjectUtils.identityToString(this);
private final ClassPathBeanDefinitionScanner scanner;
private final DefaultBeanFactory beanFactory;
private final BeanDefinitionRegistry beanDefinitionRegistry;

/**
* Constructs a new {@code AnnotationApplicationContext} for the specified base packages.
*
* @param basePackages the base packages to scan for component classes
*/
public AnnotationApplicationContext(String... basePackages) {
this.beanDefinitionRegistry = new BeanDefinitionRegistryImpl();
beanFactory = new DefaultBeanFactory();
this.scanner = new ClassPathBeanDefinitionScanner(beanFactory);
this.scanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry);
scanner.scanPackages(basePackages);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.codeus.winter.config.impl;

import com.codeus.winter.config.BeanDefinition;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class BeanDefinitionRegistryImplTest {
private BeanDefinitionRegistryImpl beanFactory;

@BeforeEach
void setUp() {
beanFactory = new BeanDefinitionRegistryImpl();
}

@Test
@DisplayName("Should create a bean using a mocked BeanDefinition")
void testCreateBeanWithMockBeanDefinition() {
BeanDefinition mockBeanDefinition = mock(BeanDefinition.class);
when(mockBeanDefinition.getBeanClassName()).thenReturn("test.MyTestBean");
when(mockBeanDefinition.isSingleton()).thenReturn(true);

beanFactory.registerBeanDefinition("testBean", mockBeanDefinition);

Object bean = beanFactory.getBeanDefinition("testBean");
assertNotNull(bean, "Bean should not be null");
}

@Test
@DisplayName("Should register and retrieve a BeanDefinition successfully")
void testRegisterAndRetrieveBeanDefinition() {
BeanDefinition mockBeanDefinition = mock(BeanDefinition.class);
when(mockBeanDefinition.getBeanClassName()).thenReturn("test.MyTestBean");

beanFactory.registerBeanDefinition("testBean", mockBeanDefinition);

BeanDefinition retrievedDefinition = beanFactory.getBeanDefinition("testBean");

assertNotNull(retrievedDefinition, "BeanDefinition should not be null");
assertEquals("test.MyTestBean", retrievedDefinition.getBeanClassName());
}

@Test
@DisplayName("Should check if a BeanDefinition exists in the registry")
void testContainsBeanDefinition() {
BeanDefinition mockBeanDefinition = mock(BeanDefinition.class);

beanFactory.registerBeanDefinition("testBean", mockBeanDefinition);

assertTrue(beanFactory.containsBeanDefinition("testBean"), "Registry should contain 'testBean'");
assertFalse(beanFactory.containsBeanDefinition("nonExistentBean"), "Registry should not contain 'nonExistentBean'");
}

@Test
@DisplayName("Should remove a BeanDefinition from the registry")
void testRemoveBeanDefinition() {
BeanDefinition mockBeanDefinition = mock(BeanDefinition.class);

beanFactory.registerBeanDefinition("testBean", mockBeanDefinition);
beanFactory.removeBeanDefinition("testBean");

assertFalse(beanFactory.containsBeanDefinition("testBean"), "Registry should not contain 'testBean' after removal");
}
}
3 changes: 2 additions & 1 deletion version.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ext {
'log4j': '2.23.1',
'junit': '5.10.3',
'checkstyle': '10.20.0',
'jacoco': '0.8.12'
'jacoco': '0.8.12',
'mockito': '5.5.0'
]
}

0 comments on commit 3250ed4

Please sign in to comment.