@SpringBootTest使用主类标记@Configuration

时间:2019-05-08 03:12:52

标签: spring-boot integration-testing spring-test

我有一个具有以下结构的Spring Boot应用程序:

App.java

import numpy as np
import math

# neurons
n_in = 3
n_hidden = 4
n_out = 1
batchsize = 60

def sigmoid(x, deriv=False):
    if deriv:
        return x*(1-x)
    return 1/(1+np.exp(-x))

def error(expected, actual):
    rawError = expected - actual
    for cell in rawError:
        cell = cell * cell
    return rawError

# input data
X = np.array([
    [0, 0, 1],
    [1, 1, 1],
    [1, 0, 1],
    [0, 1, 1]
])

# answer data
Y = np.array([0, 1, 1, 0]).T

np.random.seed(0)

# synapses
syn0 = 2 * np.random.random((n_in, n_hidden)) - 1
syn1 = 2 * np.random.random((n_hidden, n_out)) - 1

# train
for j in range(60000):
    # feed forward to hidden
    l1 = sigmoid(np.dot(X, syn0))
    # feed forward to out
    l2 = sigmoid(np.dot(l1, syn1))
    # calculate error in new array
    l2_error = error(Y, l2)
    if j % 10000 == 9999:
        print(np.sum(l2_error))

    # gradient descent:
    # multiply the error by the input, then the gradient of sigmoid
    l2_nudge = l2_error * sigmoid(l2, deriv=True)
    l1_nudge = l2_nudge.dot(syn1.T) * sigmoid(l1, deriv=True)
    syn1 += l1.T.dot(l2_nudge)
    syn0 += l0.T.dot(l1_nudge)
print(l2)

在这里您可以看到我正在通过组件扫描加载标有@Configuration的类。这些文件用于创建bean和构建ApplicationContext

我正在尝试使用以下结构配置集成测试:

Traceback (most recent call last):
  File "neural-network.py", line 68, in <module>
    l1_nudge = l2_nudge.dot(syn1.T) * sigmoid(l1, deriv=True)
ValueError: shapes (4,4) and (1,4) not aligned: 4 (dim 1) != 1 (dim 0)

运行此测试时,我得到

@SpringBootApplication(exclude = { ErrorMvcAutoConfiguration.class })
@ComponentScan(basePackages = "com.mergg.webapp.config")
public class App extends SpringBootServletInitializer{

    public static void main(final String... args) {
        new SpringApplicationBuilder(App.class)
        .initializers(new MyApplicationContextInitializer())
        .run(args);
    }

}

如何配置测试以使用此结构加载ApplicationContext?我知道我可以使用@RunWith(SpringRunner.class) @SpringBootTest(classes = App.class) public class SpringBootIntegrationTest { @Test public void whenSpringContextLoaded_thenNoExceptions() { } } 内的java.lang.IllegalStateException: Failed to load ApplicationContext 变量,但是随着我添加更多@Configuration类,我感觉这不能很好地扩展。有没有一种方法可以更简洁,更轻松地进行管理?

编辑1

这是完整的堆栈跟踪:

classes

请注意,我在集成测试和App.class中都添加了@SpringBootTest(即使App.java不直接使用该属性文件(其他配置类也使用了该属性文件,但是它们已经在其中添加了注释)该类直接)

编辑2

我所有的属性文件都在/ src / main / resources

0 个答案:

没有答案