【问题标题】:How to execute this code using dependency injection, spring and maven如何使用依赖注入、spring和maven执行这段代码
【发布时间】:2017-07-10 12:05:48
【问题描述】:

我是 Spring、Maven 和单元测试 (TDD) 的新手,我必须在新项目中同时使用所有 3 个。
过去一天我一直在阅读教程和代码示例,但找不到解决方案。

对于这个例子(一个计算器),假设我有以下接口:

public interface CalculatorService {
  public double add(double input1,
                    double input2);

  public double substract(double input1,
                          double input2);
}

这是接口的实现,依赖注入风格:

public class MathApplication {
  private CalculatorService calcService;

  public void setCalcService(final CalculatorService calcService) {
    this.calcService = calcService;
  }

  public double add(final double input1,
                    final double input2) {
    return this.calcService.add(input1, input2);
  }

  public double substract(final double input1,
                          final double input2) {
    return this.calcService.substract(input1, input2);
  }
}

测试类:

@RunWith(MockitoJUnitRunner.class)
public class MathApplicationTester {
    @Rule
    public final ExpectedException exception = ExpectedException.none();

  @InjectMocks // Marks where to inject the mocks.
  MathApplication mathApplication = new MathApplication();

  @Mock // Marks the mocks to be injected.
  CalculatorService calcService;

  @Test
  public void whenAddCorrectThenDoOperation() { 
    // GIVEN

    // WHEN
    when(this.calcService.add(10.0, 20.0)).thenReturn(30.00); // Adds the behaviour of calc service
                                                              // to add two numbers.

    // THEN
    assertThat(this.mathApplication.add(10.0, 20.0)).isEqualTo(30.0); // Tests the functionality
                                                                      // added.
    verify(calcService, times(1)).add(10.0, 20.0);
  }

    @Test
    public void whenSubstractIsCorrectThenDoOperation() throws Exception {
        // GIVEN

        // WHEN
        when(calcService.substract(20.0, 8.0)).thenReturn(12.0);

        // THEN
        assertThat(mathApplication.substract(20.0, 8.0)).isEqualTo(12.0);
    }
}

这是主要的,就像我现在拥有的那样:

@SpringBootApplication
public class TddMockitoApplication {

    public static void main(String[] args) {
        SpringApplication.run(TddMockitoApplication.class, args);
    }
}

我的问题是:如果我想进行加法或减法,我必须在 main 中添加什么,或者如何运行此代码。

【问题讨论】:

    标签: java spring maven unit-testing dependency-injection


    【解决方案1】:

    如果我没听错的话,你想使用 spring 运行这个应用程序。让我们开始吧。

    Spring为了进行依赖注入,它需要读取你的依赖配置,Spring在你标记的类上做这个读取注释(之前通过读取XML配置)

    开始使用 spring 的最佳方式是使用Spring-Boot

    步骤 1. 在 pom.xml 中添加 Spring 依赖项

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    步骤 2. 创建 Spring boot 主类

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    }

    当您在类路径中添加 spring 依赖项并在 Java 类 spring 系统上添加 @SpringBootApplication 注解时,将其识别为 spring 应用程序。它扫描相关包并准备依赖注入。

    步骤 3. 创建 Spring 配置

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    
    @Component
    public class Config {
    
        @Bean
        public CalculatorService getCalculatorService(){
            CalculatorService calcService = new CalculatorServiceImpl();
            return calcService;
        }
    }

    这告诉 Spring 如何实例化 CalculatorService。

    步骤 4. 自动装配依赖

    public class MathApplication {
        
        private CalculatorService calcService;
    
        @Autowired
        public void setCalcService(final CalculatorService calcService) {
            this.calcService = calcService;
        }
    
        public double add(final double input1,
                          final double input2) {
            return this.calcService.add(input1, input2);
        }
    
        public double substract(final double input1,
                                final double input2) {
            return this.calcService.substract(input1, input2);
        }
    }

    现在您可以从 IDE 或通过 Maven 运行此应用程序。

    附言 当有人要求您进行 TDD 时,这并不意味着您需要从主类运行测试,这意味着您应该先编写测试然后编写业务逻辑,但您只从主类运行实际的业务逻辑。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      如果您的项目的主要目标是使用 TDD 和 JUnit,那么您可以通过右键单击 IDE 中的 MathApplicationTester 类并使用 JUnit 运行它来运行您的单元测试。

      【讨论】:

      • 我已经在运行测试了,问题是运行整个应用程序并使其工作。现在测试确实有效,但我无法进行操作。
      【解决方案3】:

      如果您想运行单元测试,那么您可以右键单击该类并使用 JUnit 运行它。 如果你想做加法或减法,那么你要么必须创建一个网页,你可以在其中接受输入,然后单击添加/减法按钮,它将调用你的方法,或者你可以将加法/减法方法公开为 web服务并打电话给他们。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-06
        • 2015-03-01
        • 2013-08-09
        • 2015-03-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多