如何测试一个在Java中需要另一个服务的Maven服务?

huangapple 未分类评论84阅读模式
标题翻译

How to test a maven service which needs another service in java?

问题

我已经搜索了两天以寻找答案。我尝试了@Mock、@InjectMock和@Spy,并尝试了不同的附加操作,但是没有任何一个按照教程中描述的那样工作。我找到的许多问题都涉及Angular中的问题,这是我第一个spring-boot/maven项目,以前从未使用过Angular。

我有一个基本的服务用于一些我希望只能在那里更改的值,但是需要在多个其他服务中使用它们。我已经编写了一个缩短版的示例,展示了我想要测试的基本结构,并且我包含了我目前找到的解决方法,但我很想知道有经验的程序员会如何处理这个问题。

我有一个配置服务:

  1. @Service
  2. public class ConfigService {
  3. private final int initialLength = 4;
  4. public int getInitialLength() {
  5. return initialLength;
  6. }
  7. public double getInitialSquared() {
  8. return Math.pow(4, 2);
  9. }
  10. }

还有另一个使用它的服务:

  1. @Service
  2. public class OtherService {
  3. @Autowired
  4. ConfigService configService;
  5. // 目前我如何绕过它是通过重载,只测试第二个函数
  6. String printLine() {
  7. return printLine(configService.getInitialLength());
  8. }
  9. String printLine(int length) {
  10. StringBuilder output = new StringBuilder();
  11. for (int i = 0; i < configService.getInitialLength(); i++) {
  12. output.append("-");
  13. }
  14. return output.toString();
  15. }
  16. }

以及我的目前的测试:

  1. class OtherServiceTest {
  2. @InjectMocks
  3. OtherService otherService = new OtherService();
  4. @Test
  5. void printLine() {
  6. AssertEquals("----", OtherService.printLine(4));
  7. }
  8. }
英文翻译

I have searched two days for an answer. I tried @Mock, @InjectMock and @Spy with different additional things, but nothing worked how it was described in tutorials. Many Questions I found dealt with a Problem in Angular, this is my first spring-boot/maven project and have never worked with Angular.

I have a Basic Service for some values I want to be able to change only there but need them in multiple other services. I have written a shortened example of the base structure I want to test and I included the workaround I found for the moment, but would love to know how experienced programmers would go about this.

I have my configuration Service:

  1. @Service
  2. public class ConfigService {
  3. private final int initialLength = 4;
  4. public int getInitialLength() {
  5. return initialLength;
  6. }
  7. public double getInitialSquared() {
  8. return Math.pow(4,2);
  9. }
  10. }

And another Service using it:

  1. @Service
  2. public class OtherService {
  3. @Autowired
  4. ConfigService configService;
  5. // How I surpassed it for now is overloading and only testing the second function
  6. String printLine() {
  7. return printLine(configService.getInitialLength());
  8. }
  9. String printLine(int length) {
  10. StringBuilder output = new StringBuilder();
  11. for (int i = 0; i &lt; configService.getInitialLength(); i++) {
  12. output.append(&quot;-&quot;);
  13. }
  14. return output.toString();
  15. }
  16. }

And my Test for now:

  1. class OtherServiceTest {
  2. @InjectMocks
  3. OtherService otherService = new OtherService();
  4. @Test
  5. void printLine() {
  6. AssertEquals(&quot;----&quot;, OtherService.printLine(4));
  7. }
  8. }

huangapple
  • 本文由 发表于 2020年1月30日 18:32:08
  • 转载请务必保留本文链接:https://java.coder-hub.com/59983962.html
匿名

发表评论

匿名网友

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

确定