English 中文(简体)
• 如何测试使用春天的“@PostConstruct”方法的某类建筑者?
原标题:How to test constructor of a class that has a @PostConstruct method using Spring?

如果我有一门“@PostConstruct”方法的班子,那么我如何能够测试其建筑设计师,从而测试其“PostConstruct”方法。 我只能使用新的Name(第10段,第10段),因为当时它不使用春天——“PostConstruct”方法没有发射。

我在这里没有明显的东西?

public class Connection {
    private String x1;
    private String x2;

    public Connection(String x1, String x2) {
        this.x1 = x1;
        this.x2 = x2;
    }

    @PostConstruct
    public void init() {
        x1 = "arf arf arf";
    }

}


@Test
public void test() {
    Connection c = new Connection("dog", "ruff");
    assertEquals("arf arf arf", c.getX1());
}

我有类似(虽然稍加复杂)的情况,但“创伤”方法没有被击中。

最佳回答
问题回答

如果唯一由<代码>Connection管理的集装箱部分是@PostContruct方法,仅以测试方法人工将其称作:

@Test
public void test() {
  Connection c = new Connection("dog", "ruff");
  c.init();
  assertEquals("arf arf arf", c.getX1());
}

If there is more than that, like dependencies and so on you can still either inject them manually or - as Sridhar stated - use spring test framework.

必须改变物体的状况。 因此,在Junnit测试案中,在检查标的状态之后。 如果与<代码>@PostConstruct规定的国家相同,那么测试是成功的。

Spring,春天将不了解@PostConstruct和@Prestroy annotation。 为了便于登记,您必须登记“CommonAnnotationoBeanPostProcessla”,或具体说明“`a' in beanposition file”。

<bean class="或g.springframew或k.context.annotation.CommonAnnotationBeanPostProcess或" />

<context:annotation-config />





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签