如何从Maven模块获取Spring上下文

huangapple go评论78阅读模式
英文:

How to get spring context from maven module

问题

我在尝试在我的Spring Boot项目中进行测试时遇到了问题。

如您所见,我的项目分为Maven模块。 "alta-launcher"是“主”项目,它作为依赖项获取了每个其他模块。问题是,我的用户模块中的测试无法获得Spring上下文,因此我的"Autowired"字段在运行时将为null。

有没有什么想法可以配置这个项目,以便能够在每个模块中进行测试?

编辑:

@SpringBootTest(classes = CoreApplication.class)
class UserQueryTransformerImplTest {
    @Autowired
    private UserQueryTransformer userQueryTransformer;

    @Test
    void toDTO() {
        UserEntity userEntity = new UserEntity();
        userEntity.setLogin("everest");
        userEntity.setFirstName("Everest");
        userEntity.setLastName("Mountain");
        UserQueryDTO userQueryDTO = userQueryTransformer.toDTO(userEntity);

        assertEquals(userEntity.getLogin(), userQueryDTO.getLogin());
        assertEquals(userEntity.getFirstName(), userQueryDTO.getFirstName());
        assertEquals(userEntity.getLastName(), userQueryDTO.getLastName());
    }
}

注解@SpringBootTest的属性classes是无法使用的,因为我无法从用户模块访问启动器模块。而没有属性的情况下(只有@SprinBootTest),我得到的自动装配字段将为null,这是有道理的,因为我没有上下文。

英文:

I'm having an issue trying to make test in my spring-boot project.

architecture

as you can see, my project is devided with maven modules.
"alta-launcher" is the "main" project getting every other module as dependencies.
The problem is that my tests in the user module won't get the spring context so my fields "Autowired" will be null on runtime.

Any ideas how to configure this project to be able to do tests in each modules ?

Edit :

@SpringBootTest(classes = CoreApplication.class)
class UserQueryTransformerImplTest {
@Autowired
private UserQueryTransformer userQueryTransformer;

@Test
void toDTO() {
	UserEntity userEntity = new UserEntity();
	userEntity.setLogin("everest");
	userEntity.setFirstName("Everest");
	userEntity.setLastName("Mountain");
	UserQueryDTO userQueryDTO = userQueryTransformer.toDTO(userEntity);

	assertEquals(userEntity.getLogin(), userQueryDTO.getLogin());
	assertEquals(userEntity.getFirstName(), userQueryDTO.getFirstName());
	assertEquals(userEntity.getLastName(), userQueryDTO.getLastName());
}

The annotation @SpringBootTest with the attribute classes is unusable because I don't have access to the launcher module from user module. And without the attribute (juste @SprinBootTest) is when I'm getting my autowire field null which makes sense cause I don't have the context.

答案1

得分: 0

你可以在测试中重新创建自定义上下文,尝试类似这样的方法:

@ContextConfiguration
@RunWith(SpringRunner.class)
class UserQueryTransformerImplTest {
    @TestConfiguration
    static class Config {
        @Bean
        public UserQueryTransformer uqt() {
            return new UserQueryTransformer();
        }
    }

    @Autowired
    private UserQueryTransformer userQueryTransformer;

    @Test
    void toDTO() {
        UserEntity userEntity = new UserEntity();
        userEntity.setLogin("everest");
        userEntity.setFirstName("Everest");
        userEntity.setLastName("Mountain");
        UserQueryDTO userQueryDTO = userQueryTransformer.toDTO(userEntity);

        assertEquals(userEntity.getLogin(), userQueryDTO.getLogin());
        assertEquals(userEntity.getFirstName(), userQueryDTO.getFirstName());
        assertEquals(userEntity.getLastName(), userQueryDTO.getLastName());
    }
}
英文:

You can recreate your custom context in tests, try something like this:

@ContextConfiguration
@RunWith(SpringRunner.class)
class UserQueryTransformerImplTest {
@TestConfiguration
static class Config {
  @Bean
  public UserQueryTransformer uqt() {
     return new UserQueryTransformer();
  }
}
@Autowired
private UserQueryTransformer userQueryTransformer;

@Test
void toDTO() {
    UserEntity userEntity = new UserEntity();
    userEntity.setLogin("everest");
    userEntity.setFirstName("Everest");
    userEntity.setLastName("Mountain");
    UserQueryDTO userQueryDTO = userQueryTransformer.toDTO(userEntity);

    assertEquals(userEntity.getLogin(), userQueryDTO.getLogin());
    assertEquals(userEntity.getFirstName(), userQueryDTO.getFirstName());
    assertEquals(userEntity.getLastName(), userQueryDTO.getLastName());
}

答案2

得分: 0

我使用了ContextConfiguration注解来解决了这个问题。
(ExtendWith是用于JUnit 5部分)

@ContextConfiguration(classes = ConfigurationTest.class)
@ExtendWith(SpringExtension.class)
class UserQueryTransformerImplTest {
    @Autowired
    private UserQueryTransformer userQueryTransformer;

    @Test
    void toDTO() {
        UserEntity userEntity = new UserEntity();
        userEntity.setLogin("everest");
        userEntity.setFirstName("Everest");
        userEntity.setLastName("Mountain");
        UserQueryDTO userQueryDTO = userQueryTransformer.toDTO(userEntity);

        assertEquals(userEntity.getLogin(), userQueryDTO.getLogin());
        assertEquals(userEntity.getFirstName(), userQueryDTO.getFirstName());
        assertEquals(userEntity.getLastName(), userQueryDTO.getLastName());
    }
}

这是我的ConfigurationTest.java配置文件

@TestConfiguration
public class ConfigurationTest {

    @Bean
    UserQueryTransformer createUserQueryTransformer() {
        return new UserQueryTransformerImpl();
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    UserQueryDTO createUserQueryDTO() {
        return new UserQueryDTO();
    }
}
英文:

I was able to fix the issue by using ContextConfiguration annotation.
(ExtendWith is for the junit5 part)

@ContextConfiguration(classes = ConfigurationTest.class)
@ExtendWith(SpringExtension.class)
class UserQueryTransformerImplTest {
@Autowired
private UserQueryTransformer userQueryTransformer;

@Test
void toDTO() {
    UserEntity userEntity = new UserEntity();
    userEntity.setLogin("everest");
    userEntity.setFirstName("Everest");
    userEntity.setLastName("Mountain");
    UserQueryDTO userQueryDTO = userQueryTransformer.toDTO(userEntity);

    assertEquals(userEntity.getLogin(), userQueryDTO.getLogin());
    assertEquals(userEntity.getFirstName(), userQueryDTO.getFirstName());
    assertEquals(userEntity.getLastName(), userQueryDTO.getLastName());
}

and here is my configurationTest.java

@TestConfiguration
public class ConfigurationTest {

@Bean
UserQueryTransformer createUserQueryTransformer() {
	return new UserQueryTransformerImpl();
}

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
UserQueryDTO createUserQueryDTO() {
	return new UserQueryDTO();
}

huangapple
  • 本文由 发表于 2020年8月20日 21:23:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63506098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定