简体   繁体   English

JUnit使用Mocked类进行测试

[英]JUnit tests with Mocked classes

I need solution to write Junit test case for following method. 我需要解决方案为以下方法编写Junit测试用例。 all the classes are in different files and i want to write test case for method2 of class A . 所有类都在不同的文件中,我想为AA method2编写测试用例。

How to use @mock in this case? 在这种情况下如何使用@mock

class A {
  static String method1(string a)
  {
    B b=new B;
    //some changes applied on a
    return b.method3(a);
  }

  String method2(String a)
  { 
    return A.method1(a);
  }
}

class B {
  String method3(String a)
  {
    C c=new C();
    //some changes applied on a
    return c.method4(a);
  }
}

class C {
  String method4(String a)
  {
    // some changes applied on a
    return a;
  }
}

Ideally, you should only test the layer you are concerned with but if you want to call class A , B and C method in single test case below is the solution. 理想情况下,您应该只测试您关注的层,但如果您想在下面的单个测试用例中调用A类,B和C方法,那么解决方案就是解决方案。 This solution si smore like integration test case. 这个解决方案就像整合测试案例一样。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class TestA {

    @Spy
    @InjectMocks
    private B b = new B();
    @Spy
    private C c = new C();  
    @InjectMocks
    private A a = new A();

    @Test
    public void testMethod1() {
        String val = a.method1("test");
        System.out.println(val);
    }   
}

class A {
    public B b;
    public String method2(String a){
        System.out.println("Inside method A.method2");
        return b.method3(a);
    }
    public String method1(String a){ 
        System.out.println("Inside method A.method1");
        return method2(a);
    }
}

class B {
    public C c;
    public String method3(String a){
        System.out.println("Inside method B.method3");
        return c.method4(a);
    }
}

class C {
    public String method4(String a){
        System.out.println("Inside method C.method4");
        return a;
    }
}

output will be as below 输出如下

Inside method A.method1
Inside method A.method2
Inside method B.method3
Inside method C.method4
test

PS you can use @Mock instead of spy then remove the new operator of that field and Mock the behaviour of that class method. PS你可以使用@Mock而不是spy然后删除该字段的新运算符并模拟该类方法的行为。

There is some work to be done. 还有一些工作要做。 First, dependent class must be injected using some IoC system, like using @Autowired or @Inject : 首先,必须使用某些IoC系统注入依赖类,例如使用@Autowired@Inject

class A{

    @Autowired
    private B b;

    String method1(string a) {
       //some changes applied on a
       return b.method3(a);
    }

    String method2(String a){ 
       return method1(a);
    }
}

Then, you have to mock in a test like the following: 然后,您必须在以下测试中进行模拟:

@RunWith(MockitoJUnitRunner.class)
public class TestA {

@Mock
private B b;

@InjectMocks
private A a;

public void testMethod1() {

    /* test code here */

    }

}

Also consider that B should be an interface, not a concrete class. 还要考虑B应该是一个接口,而不是具体的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM