如何在没有主类的情况下在Spring Boot应用程序中创建测试(使用Mockito)

发布于 2025-02-12 18:23:19 字数 1171 浏览 0 评论 0原文

我有一个库,该项目是用Springbot编写的,但是这里无需在此处使用主类,

interface Helper{
  String transform(int index);
}
class HelperImpl implements Helper{

private final Convert convert();

  public Helper(Convert convert){

  this.convert = convert.
 }

 public String transform(int index){

   int res = convert.print(index);

   return  String.valueOf(res);
 }
}

interface Convert {
int print(int value);

}
class ConvertImpl implements Convert{

public int print(int value){
    return 1;
  }

}
@DisplayName("Message ")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Slf4j
class Test {

    @Autowired
    Helper helper;

    @Autowired
    Convert convert;


    @BeforeAll
    void init() {
      }

    @Test
    void testRun() {
      
     String result = helper.transform(1);

  }
}

我该如何编写集成测试,在升高的过程中,将自动进行注入,我只需要通过接收的实例,通过值,获取结果并进行检查。这是相同的注入,并且不起作用。

I have a library, the project is written in springbot, but there is no need to use main class here

interface Helper{
  String transform(int index);
}
class HelperImpl implements Helper{

private final Convert convert();

  public Helper(Convert convert){

  this.convert = convert.
 }

 public String transform(int index){

   int res = convert.print(index);

   return  String.valueOf(res);
 }
}

interface Convert {
int print(int value);

}
class ConvertImpl implements Convert{

public int print(int value){
    return 1;
  }

}
@DisplayName("Message ")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Slf4j
class Test {

    @Autowired
    Helper helper;

    @Autowired
    Convert convert;


    @BeforeAll
    void init() {
      }

    @Test
    void testRun() {
      
     String result = helper.transform(1);

  }
}

How can I write an integration test, during the raising of which an inject would be automatically made and I would only have to call methods through the received instances, pass values, get the result and check it. This is the same inject and it does not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2025-02-19 18:23:19

@autowed注释是弹簧注释,因此,如果没有加载弹簧上下文,就无法使用。
有三种常用的解决方案可以提供测试类所需的类所需的类的实例:

手动实例化

您不使用spring,并使用 new keyword进行实例化,或将Mockito用于依赖关系必须嘲笑。构造函数注入比现场注入更优选,因为它可以使您轻松绕过单元测试中的弹簧。

PROS :运行速度更快。更容易配置
cons:有时可以手动提供所有必需的依赖项可能很复杂。如果您的代码严重依赖于春季功能,那么在某些情况下,使用此解决方案可能会更复杂。

使用@springboottest,

您想依靠在运行时使用的春季配置,然后使用Springboot解决方案: @springboottest 注释和 @mockbean 您要模拟的依赖项的注释。
PROS:促进依赖注入。弹簧功能可以在测试中使用。用@mockbean注释嘲笑依赖项很容易。
cons:需要弹簧应用程序上下文,并且在大型项目中初始化可能会很慢。请注意,如果不必更改,则将在测试之间重复使用。

使用SpringExtension,

您必须提供一种弹簧配置,该配置将定义您要自动的豆类。

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class MyTest { 
  
  @Autowired
  Helper helper;

  ...
}

@Configuration
public class SpringTestConfiguration {

  @Bean
  public Helper helper() { return new Helper(); }
  }
}

The @Autowired annotation is a Spring annotation, hence cannot be used without a Spring context being loaded.
There are three usual solutions to provide the instances of the classes required by the classes under test :

Manual instantiations

You don't use Spring, and instantiate the dependencies using the new keyword, or using Mockito for the dependencies that have to be mocked. Constructor injection is preferred to field injection, as it will allow you to easily bypass Spring in your unit tests.

Pros : Faster to run. Easier to configure
Cons : It can sometimes be complex to manually provide all the required dependencies. If your code heavily relies on Spring features, then, in some situations, it can be a bit more complex to use this solution.

Using @SpringBootTest

You want to rely on the same Spring configuration that you will use at runtime, then you should use the Springboot solution : the @SpringBootTest annotation, and the @MockBean annotation for the dependencies that you want to mock.
Pros : Facilitate the dependency injection. Spring features can be used in the tests. It's easy to mock the dependencies with the @MockBean annotation.
Cons : The Spring application context is required and can be slow to initialize in large projects. Note that it will be reused between tests if it doesn't have to change.

Using SpringExtension

You'll have to provide a Spring configuration that will define the beans that you want to autowire.

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class MyTest { 
  
  @Autowired
  Helper helper;

  ...
}

@Configuration
public class SpringTestConfiguration {

  @Bean
  public Helper helper() { return new Helper(); }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文