如何使用依赖注入,spring和maven执行此代码

时间:2017-07-10 12:05:49

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

我是Spring,Maven和Unit Testing(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);
    }
}

这就是Main,正如我现在所说的那样:

@SpringBootApplication
public class TddMockitoApplication {

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

我的问题是:如果让我说要添加或减少,我需要将什么内容放入main中,或者如何运行此代码。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

If I get you correctly you want to run this application using spring. Let get Started.

Spring in order to make dependency injection it needs to read configuration for your dependency, Spring does this reading annotation on classes that you mark( Earlier by reading XML config)

The best way to start with spring is to use Spring-Boot.

Step 1. Add Spring dependency in your pom.xml

<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>

Step 2. Create Spring boot main class

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);
    }
}

When you add spring dependency in classpath and add @SpringBootApplication annotation on Java class spring system identify this as spring application. it scans relevant packages and prepares for dependency injection.

Step 3. Create Spring configuration

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;
    }
}

This tells Spring how to instantiate CalculatorService.

Step 4. Autowire dependecy

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);
    }
}

Now you can run this application from your IDE or via Maven.

P.S When someone ask you to TDD it doesn't mean that you need to run the test from main class, it means that you should write the test first and then write the business logic, but you only run actual business logic from the main.

Hope this helps.