简体   繁体   English

使用Mockito测试@Postconstruct

[英]Testing @Postconstruct with Mockito

Why when I injecting mocks via Mockito my @Postconstruckt method is not calling? 为什么当我通过Mockito注入Mockito我的@Postconstruckt方法没有调用?

@Service
public class MyService {
    public MyService() {
        System.out.println("CONSTRUKTOR");
    }

    @PostConstruct
    public void init() {
        System.out.println("POST CONSTRUCT");
    }

@RunWith(MockitoJUnitRunner.class)
public class Mockito1 {

    @InjectMocks
    private MyService service;

    @Before
    public void init() {
    }

Output: Only: CONSTRUKTOR 输出:仅: CONSTRUKTOR

Because PostConstruct is only spring concept. 因为PostConstruct只是spring的概念。 But you can call postConstruct manually. 但是您可以手动调用postConstruct。

 @Before public void prepare() { MockitoAnnotations.initMocks(this); this.service.init(); //your Injected bean } 

@PostConstruct is an annotation defined by JSR 250 and it will be ignored in your current test cause you are using @RunWith(MockitoJUnitRunner.class) , ie, A JUnit runner that is not aware of this annotation. @PostConstruct是由JSR 250定义的注释,由于您正在使用@RunWith(MockitoJUnitRunner.class) ,因此,它将在当前测试中被忽略,即,一个不知道此注释的JUnit运行器。 If you are writing unit tests they should be simple ad just test the business logic of your application, but if you want to write integrations tests that use some third-party code that is able to process this kind of annotation, like Spring, for example. 如果要编写单元测试,它们应该是简单的广告,只需测试应用程序的业务逻辑即可;但是,如果要编写集成测试,该测试使用一些能够处理这种注释的第三方代码,例如Spring。 。 Then you can annotate your test class with following annotations: 然后,您可以使用以下注释来注释您的测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class})

I modified a little your service by adding a method foo: 我通过添加方法foo修改了一些服务:

@Service
public class MyService {
    public MyService() {
        System.out.println("CONSTRUKTOR");
    }

    @PostConstruct
    public void init() {
        System.out.println("POST CONSTRUCT");
    }

    public String foo() {
        return "bar";
    }
}

If you want to get behaviour, that you described, there are at least two possibilities: 如您所描述的,如果您想要获得行为,则至少有两种可能性:

  1. @RunWith(SpringJUnit4ClassRunner.class) + @Autowired - that combination will let you to get a usual service in your test @RunWith(SpringJUnit4ClassRunner.class) + @ @Autowired该组合将使您能够在测试中获得常规服务

     @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MyService.class) public class Mockito1 { @Autowired private MyService service; @Before public void init() { } @Test public void name() throws Exception { System.out.println(service.foo()); } } 

This code will print: 此代码将打印:

CONSTRUKTOR
POST CONSTRUCT
bar
  1. @RunWith(SpringJUnit4ClassRunner.class) + @SpyBean - that combination will let you to get a service in your test and to modify it's behaviour using Mockito @RunWith(SpringJUnit4ClassRunner.class) + @SpyBean该组合将使您能够在测试中获得服务并使用Mockito修改其行为

     @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MyService.class) public class Mockito1 { @SpyBean private MyService service; @Before public void init() { } @Test public void name() throws Exception { System.out.println(service.foo()); Mockito.when(service.foo()).thenReturn("FOO"); System.out.println(service.foo()); } } 

This code will print: 此代码将打印:

CONSTRUKTOR
POST CONSTRUCT
bar
FOO

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

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