How do I check multiple #values in one test class? - spring

Code to be tested
public Hoge {
public Hoge(#Value("${hoge.val:30}")final int p1) { // Constructor
this.p1 = p1;
I want to check p1 when hoge.val is specified or not.
I want to run the same tests as below in one test class.
How do I write a test case?
Test class
#SpringBootTest(properties="hoge.val=20")
public Hoge1Test {
...
#SpringBootTest(properties="hoge.val=")
public Hoge2Test {
...

Related

Equivalent of generic NUnit.Framework.TestFixture in xUnit

In NUnit, we can create a generic test fixture.
[TestFixture(typeof(double))]
[TestFixture(typeof(float))]
public class ServiceTests<T>
{
private readonly MyService<T> myService = new();
/* ... */
}
Has xUnit any equivalent of this?
I'm trying to test a generic service and now I need to create separate classes to test it for many generic types.
public abstract class DoubleServiceTests : ServiceTests<double>
{}
public abstract class FloatServiceTests : ServiceTests<float>
{}
public abstract class ServiceTests<T>
{
private readonly MyService<T> myService = new();
/* ... */
}
This is a solution that I'm currently using.
Maybe usage of Theory attribute will help.
Theory allows injection of test data inside a test. So some test from ServiceTests may look like this:
[Theory]
[InlineData(typeof(float))]
[InlineData(typeof(double))]
public void SomeTestMethod(Type t)
{
// make use of type t
}
More [info] on Theory related attributes in xunit.

Does Juint reset global variables for each test?

I have an integration test class like this (but more complex)
#ExtendWith(SpringExtension::class)
#WebMvcTest(Controller::class)
class ScheduleControllerTest(#Autowired val mockMvc: MockMvc) {
val obj = object {
var b = 2
}
#Test
fun a(){
obj.b = 1
assertEquals(obj.b, 1)
}
#Test
fun b(){
assertEquals(obj.b, 2)
}
}
and all tests in this class pass as if after each test obj is reset to its initial value. No #Before functions are used here, so what is happening?
By default JUnit creates a new test class instance for each test run (fields are initialized anew each time). You can modify this behavior by using the #TestInstance annotation.

How to enable the scheduler in spring boot based on the static variable at runtime?

Here are the details,
I have two schedulers with #scheduled Annotation. Let's says A & B. "A" Scheduler does some job and set value in static variable using setter. I want to enable the "B" Scheduler only the static variable is set to true.
One solution, I know is to the check/validate the static variable flag in "B" scheduler and skip the further process but the scheduler checks every time based on interval specified (i.e., fixed delay).
I want to completely disable the scheduler based on static variable at run time value.
PS : I manually set the time delay between the "A" & "B" Schedulers. So "A" runs first then "B"
For Example:
Initialize.class
Configuration
EnableScheduling
public class Initialize {
public A a{
return new A();
}
public B b{
return new B();
}
}
A.class
public class A{
Scheduled(fixedDelayString = "sometime specified")
public void aProcessor(){
// Following code sets the value of static variable like staticClass.setAppVariable(true);
}
}
B.class
public class B{
Scheduled(fixedDelayString = "sometime specified")
public void bProcessor(){
// some process
}
}
static.class
public class staticClass implements Serializable{
private static volatile boolean appVariable = false;
public static boolean getAppVariable() {
return appVariable;
}
public static void setAppVariable(boolean appVariable) {
staticClass.appVariable = appVariable;
}
}
But I need to initialise the "B" class/scheduler based on the static variable if it is true.
Any help?

Test case for constructor as i have a filter logic

I have constructor where there is filter logic and wanna test it, though writing test case for constructor is not in practice i wanna have the code coverage , have tried many links but none are explaining about handling a constructor.
public Myclass {
public Myclass(AnotherClass obj)
{
_key = obj.key;
_ID = obj.ID;
_CandidateMode = obj.CandidateMode;
if(_CandidateMode == obj.CandidateMode.numeric
{
//Dosomething
}
else
{
//Do something with special character.
}
}
}
Definitely placing logic inside a constructor is a thing to avoid. Good you know that :-) In this particular case maybe the if could go into each of the public methods of MyClass, or maybe you could use polymorphism (create MyClass or MySpecialCharacterClass base on the AnotherClass object)?
Anyway, to get to a straight answer: if you really must test constructor logic, do it like you would test any other method (in some languages it's just a static method called new, by the way).
[TestMethod]
public void is_constructed_with_numeric_candidate() {
// Given
AnotherClass obj = new AnotherClass { CandidateMode = CandidateMode.numeric };
// When
MyClass myClass = new MyClass(obj);
// Then
// assert myClass object state is correct for numeric candidate
...
}
[TestMethod]
public void is_constructed_with_special_candidate() {
// Given
AnotherClass obj = new AnotherClass { CandidateMode = CandidateMode.special };
// When
MyClass myClass = new MyClass(obj);
// Then
// assert myClass object state is correct for special candidate
...
}

How to reuse PHPUnit functions

I want to reuse some of my PHPUnit functions by calling them instead of repeating them in all unit tests, such as this for example:
public function testContent()
{
$this->assertNotEmpty($this->response, $this->message);
}
If I place it under tests/TestCase.php, it runs for all the unit tests.
Where would be the right place to put this or how is this usually done? Btw, I am using it on Laravel 4.
Apparently, your problem is not to re-use tests, but that your tests are beeing called when it's not expected.
So, have in your mind: any method with a name starting with "test" (testFoo, testBar) in an existing class will be considered as a test.
So if you have classes A, B and C like this
class A extends \PHPUnit_Framework_TestCase {
public function testFoo() {...}
public function testBar() {...}
}
class B extends A {
public function testBaz() {...}
}
class C extends A {
public function testQuz() {...}
}
you will have 8 tests: A::testFoo(), A::testBar(), B::testFoo(), B::testBar(), B::testBaz(), C::testFoo(), C::testBar(), C::testQuz().
It's probably is not what you was trying to make.
You may want to have testFoo and testBar only in classes B and C, not it A. In this case, you just have to declare A as an abstract class.
abstract class A extends \PHPUnit_Framework_TestCase {
public function testFoo() {...}
public function testBar() {...}
}
class B extends A {
public function testBaz() {...}
}
class C extends A {
public function testQuz() {...}
}
Now you have 6 tests: B::testFoo(), B::testBar(), B::testBaz(), C::testFoo(), C::testBar(), C::testQuz().
Maybe, it's what you need, maybe not. Maybe, you want to have testFoo() and testBar() only in some inherited classes, not all of them.
In this case, make assertions in you parent class instead of tests.
abstract class A extends \PHPUnit_Framework_TestCase {
public function assertFoo() { ... }
public function assertBar() { ... }
}
class B extends A {
public function testFoo() { $this->assertFoo(); ...}
public function testBaz() {...}
}
class C extends A {
public function testBar() { $this->assertBar(); ...}
public function testQuz() {...}
}
Now, you have only 4 tests: B::testFoo(), B::testBaz, C::testBar(), C::testQuz()
Make your own assertion according to this manual
Make something like
namespace Vendor\Library\Tests;
abstract class BaseTestCase extends \PHPUnit_Framework_TestCase
{
public function assertContentNotEmpty($response) {
$this->assertNotEmpty($response);
}
}
And then extend this class with your tests.
If this approach is not convenient for some reasons, you can make an assertion as a separate class.

Resources