编写一个针对没有参数的布尔方法的 JUnit 测试。

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

Writing a Junit test for a boolean method with no parameters

问题

  1. 我有一个类我想为它编写一个 JUnit 测试
  2. 这个方法没有参数可以相应地进行测试吗
  3. ```java
  4. public class classTobeTested {
  5. @Self
  6. SlingHttpServletRequest request;
  7. static final String keyword = "hello";
  8. public boolean isActive() {
  9. boolean check;
  10. String pathChecker;
  11. pathChecker = (request.getRequestURL()).toString();
  12. check = pathChecker.contains(keyword);
  13. return check;
  14. }
  15. }

以下是我设想的测试类:

  1. @RunWith(MockitoJUnitRunner.class)
  2. public class testclasstobetested {
  3. @Test
  4. public void TestclassTobeTested() throws Exception {
  5. classTobeTested CTT = new classTobeTested();
  6. assertFalse(CTT.isActive("hello how are you"));
  7. }
  8. }

我知道我的方法没有接受参数,但方法内部声明了字符串。
我该如何正确使用 assertFalse 来测试没有参数的方法呢?

  1. <details>
  2. <summary>英文:</summary>
  3. I have a class where I want to write a junit test for.
  4. This method has no parameters, can this method accordingly?

public class classTobeTested {

  1. @Self
  2. SlingHttpServletRequest request;
  3. static final String keyword = &quot;hello&quot;;
  4. public boolean isActive() {
  5. boolean check;
  6. String pathChecker;
  7. pathChecker = (request.getRequestURL()).toString();
  8. check= pathChecker.contains(keyword);
  9. return check;
  10. }

}

  1. This would be the testing class i had in mind

@RunWith(MockitoJUnitRunner.class)
public class testclasstobetested {

  1. @Test
  2. public void TestclassTobeTested() throws Exception{
  3. classTobeTested CTT = new classTobeTested();
  4. assertFalse(CTT.isActive(&quot;hello how are you&quot;));
  5. }

}

  1. I know my method does not take a parameter but has strings declared inside the method.
  2. How can i use assertFalse correctly to test a non param method.
  3. </details>
  4. # 答案1
  5. **得分**: 2
  6. 使用注解和Junit4,您可以像这样完成:
  7. ```java
  8. @RunWith(MockitoJUnitRunner.class)
  9. public class testclasstobetested {
  10. @InjectMocks
  11. private classTobeTested CTT;
  12. @Mock
  13. private SlingHttpServletRequest request;
  14. @Test
  15. public void TestclassTobeTested() throws Exception{
  16. when(request.getRequestURL()).thenReturn(new StringBuffer("hello how are you"));
  17. assertFalse(CTT.isActive());
  18. }
  19. }
英文:

Using annotations and Junit4 you can do it like this:

  1. @RunWith(MockitoJUnitRunner.class)
  2. public class testclasstobetested {
  3. @InjectMocks
  4. private classTobeTested CTT;
  5. @Mock
  6. private SlingHttpServletRequest request;
  7. @Test
  8. public void TestclassTobeTested() throws Exception{
  9. when(request.getRequestURL()).thenReturn(new StringBuffer(&quot;hello how are you&quot;));
  10. assertFalse(CTT.isActive());
  11. }
  12. }

huangapple
  • 本文由 发表于 2020年7月27日 14:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63109529.html
匿名

发表评论

匿名网友

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

确定